在 ASP.NET 中调用远程 Web 服务
我正在尝试调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过使用它,它更简单:
这样您就可以将 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:
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!