2

geonames Web サービスを呼び出して、結果を json 形式で返そうとしています。ネットで httpwebrequest を使用するチュートリアルをいくつか見つけましたが、msdn ではこれは廃止されていると書かれています。私のコードが Web リクエストに到達すると、タイムアウトし続けます。何か案は?私の .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;
    }
}
4

1 に答える 1

4

代わりにこれを使用してみましたか?

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

そうすれば、JSON を文字列としてダウンロードできます。

また、URLを入れてみましたか

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

あなたの場所をフィドラーのようなツールに - http://www.fiddler2.com/fiddler2/ ?

サービスが実際にタイムアウトしているか、リクエストの作成方法が正しくない可能性があります。そうすれば、それがサービスなのかコードなのかを排除できます。

また、誰もあなたのユーザー名を使用してサービスを呼び出すことができないように、質問からユーザー名を削除することもできます!

于 2011-02-28T16:43:48.607 に答える