簡體   English   中英

在asp.net中調用遠程Web服務

[英]call remote web service in asp.net

我正在嘗試調用地名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;
    }
}

您是否嘗試過使用它,它要簡單得多:

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

這樣,您可以將JSON下載為字符串。

另外,您是否嘗試過輸入網址

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

將您的位置放到提琴手之類的工具中-http: //www.fiddler2.com/fiddler2/

可能是服務實際上正在超時,或者您構建請求的方式不太正確。 這樣,您可以消除是服務還是代碼。

另外,您可能想從問題中刪除用戶名,只是沒有人可以使用您的用戶名致電服務!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM