当使用httpWebResponse时,我似乎无法得到任何回报

本文关键字:回报 任何 httpWebResponse | 更新日期: 2023-09-27 18:05:44

我使用以下代码:

var request = (HttpWebRequest)WebRequest.Create(new Uri(addy));
request.UserAgent = "Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))";
request.BeginGetResponse(
    r =>
    {
        var httpRequest = (HttpWebRequest)r.AsyncState;
        var httpResponse = (HttpWebResponse)httpRequest.EndGetResponse(r);
        using (var reader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var response = reader.ReadToEnd();
            results = (String)response;
        }
    }, request);

add is: http://maps.googleapis.com/maps/api/geocode/xml?address=limavady&sensor=false

and results只是一个全局字符串,我想输出web请求的结果。

问题只是我一直得到一个空值输出到结果,我已经检查了网址,它是正确的,应该有输出。

如果你有什么想法,或者如果你能给我指出正确的方向,我将不胜感激

当使用httpWebResponse时,我似乎无法得到任何回报

  1. 您请求的页面通过Content-Encoding: gzip向您发送数据。将NuGet包SharpZipLib.WindowsPhone7添加到您的项目中,并查看下面的示例代码。
  2. 确保你已经声明了ID_CAP_NETWORKING capability。
  3. 检查HttpWebResponse.StatusCode属性

    static Stream responseStream( this HttpWebResponse resp )
    {
        if( null == resp ) return null;
        string enc = resp.Headers[ HttpRequestHeader.ContentEncoding ];
        if( !string.IsNullOrEmpty( enc ) )
        {
            enc = enc.ToLower();
            if( enc == "gzip" )
                return new GZipInputStream( resp.GetResponseStream() );
        }
        return resp.GetResponseStream();
    }
    

感谢您的帮助,我使用以下方法解决了问题:

WebClient webClient = new WebClient();
        webClient.OpenReadAsync(new Uri(addy));
        webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
    }
    void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        var reader = new StreamReader(e.Result);
        results = reader.ReadToEnd();
    }