在 ASP.NET 中调用远程 Web 服务

发布于 2024-10-19 08:57:28 字数 1281 浏览 3 评论 0原文

我正在尝试调用 geonames Web 服务并以 json 格式返回结果。我在网上找到了一些使用 httpwebrequest 的教程,但是在 msdn 中它说这已经过时了。当我的代码到达网络请求时,它总是超时。有什么想法吗?我的 .asmx 代码如下:

 /// Summary description for Geonames
/// </summary>
[WebService(Namespace = "http://api.geonames.org")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class Geonames : System.Web.Services.WebService
{
    private readonly static string FindCoordinates = "http://api.geonames.org/postalCodeSearchJSON?placename={0}&username=<username>";
    [WebMethod]
    [System.Web.Script.Services.ScriptMethod()]
    public string getCoordinates(string location)
    {
        Uri address = new Uri(String.Format(FindCoordinates, HttpUtility.UrlPathEncode(location)));
     //   HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(address.AbsoluteUri);
     //   wr.ProtocolVersion = HttpVersion.Version10;
        string jsonResponse = string.Empty;
        WebClient client = new WebClient();
        jsonResponse = client.DownloadString(address.AbsoluteUri);

        return jsonResponse;
    }
}

I am trying to call the geonames web services and return my result in json format. I found some tutorials on the net that use httpwebrequest however in msdn it says that this is obsolete. When my code gets to the web request it keeps timing out. Any ideas? My .asmx code is below:

 /// Summary description for Geonames
/// </summary>
[WebService(Namespace = "http://api.geonames.org")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class Geonames : System.Web.Services.WebService
{
    private readonly static string FindCoordinates = "http://api.geonames.org/postalCodeSearchJSON?placename={0}&username=<username>";
    [WebMethod]
    [System.Web.Script.Services.ScriptMethod()]
    public string getCoordinates(string location)
    {
        Uri address = new Uri(String.Format(FindCoordinates, HttpUtility.UrlPathEncode(location)));
     //   HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(address.AbsoluteUri);
     //   wr.ProtocolVersion = HttpVersion.Version10;
        string jsonResponse = string.Empty;
        WebClient client = new WebClient();
        jsonResponse = client.DownloadString(address.AbsoluteUri);

        return jsonResponse;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤凫 2024-10-26 08:57:28

您是否尝试过使用它,它更简单:

        WebClient client = new WebClient();
        client.DownloadString("Your_api_location_goes_here");

这样您就可以将 JSON 作为字符串下载。

另外,您是否尝试过输入 URL

http://api.geonames.org/postalCodeSearchJSON?placename={0}&username=

将您的位置输入到 fiddler 等工具中 - http://www.fiddler2。 com/fiddler2/

可能是服务实际上超时了,或者您构建请求的方式不太正确。这样你就可以消除它是服务还是你的代码。

另外,您可能想从问题中删除您的用户名,这样就没有人可以使用您的用户名调用该服务!

Have you tried using this instead, its a lot simpler:

        WebClient client = new WebClient();
        client.DownloadString("Your_api_location_goes_here");

That way you can download the JSON as a string.

Also, Have you tried putting the URL

http://api.geonames.org/postalCodeSearchJSON?placename={0}&username=

with your location into a tool like fiddler - http://www.fiddler2.com/fiddler2/?

It could be that the service is actually timing out, or the way you are building the request isnt quite right. That way you could eliminate whether it is the service or your code.

Also, you might want to remove your username from your question, just so that no-one can call the service using your username!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文