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