我正在尝试调用 geonames 网络服务并以 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;
}
}