1

我正在使用以下代码:

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);

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

results 只是一个全局字符串,我想将 Web 请求的结果输出到该字符串。

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

对此有任何想法,或者如果您能指出正确的方向,我将不胜感激

4

2 回答 2

0

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

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();
    }
于 2013-03-29T12:03:35.597 回答
0
  1. 您请求的页面会使用 向您发送数据Content-Encoding: gzip。将 NuGet 包添加SharpZipLib.WindowsPhone7到您的项目中,并在下面查看我的示例代码。
  2. 确保您已声明 ID_CAP_NETWORKING 功能。
  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();
    }
    
于 2013-03-26T16:50:21.653 回答