2
我想调用geonames web服务并以json格式返回我的结果。我在网上找到了一些使用httpwebrequest的教程,但是在msdn中它说这已经过时了。当我的代码到达Web请求时,它会超时。有任何想法吗?我的.asmx代码如下:在asp.net中调用远程web服务
/// 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;
}
}
我试过上面的webclient,我仍然无法连接到远程主机错误 – rbur0425 2011-02-28 19:28:30
这是我的工作网络搞乱了连接。当我在我的家庭网络client.downloadstring工作正常!谢谢! – rbur0425 2011-03-01 14:35:42