C# 在Visual Studio Express中为WP 2012返回异步HttpWebRequest

C# 在Visual Studio Express中为WP 2012返回异步HttpWebRequest,c#,windows-phone-7,visual-studio-2012,C#,Windows Phone 7,Visual Studio 2012,我在VS Express for WP 2012中面临HttpWebRequest的异步,我的目标是WP 7.1 我想写一个返回URL结果的方法,如下所示: public static string GetContentByPass(string link) { string result = ""; var request = (HttpWebRequest)WebRequest.Create( new Uri(link));

我在VS Express for WP 2012中面临HttpWebRequest的异步,我的目标是WP 7.1

我想写一个返回URL结果的方法,如下所示:

public static string GetContentByPass(string link)
    {
        string result = "";
        var request = (HttpWebRequest)WebRequest.Create(
            new Uri(link));
        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();
                Deployment.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    result = response;
                }));
            }
        }, request);
        return result;
    }
使用如下方式:

result = Util.GetContentByPass("http://google.com");
但结果总是空的

有什么想法吗

谢谢:-)

编辑

这是我的解决方案,希望它能帮助需要帮助的人:-)

公共静态字符串GetContentFromURL(字符串url)
{
var task=GetResult(url);
返回任务。结果;
}
私有静态任务GetResult(字符串url)
{
url=url.Replace(“http:/”,“”);
url=“http://”+url;
HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(url);
request.Method=“GET”;
request.UserAgent=“Mozilla/5.0(Linux;U;Android 4.0.3;de ch;HTC Sensation Build/IML74K)AppleWebKit/534.30(KHTML,类似Gecko)版本/4.0 Mobile Safari/534.30”;
Task Task=Task.Factory.fromsync(
request.BeginGetResponse,asyncResult=>request.EndGetResponse(asyncResult),(object)null
);
返回task.ContinueWith(t=>ReadStreamFromResponse(t.Result));
}
私有静态字符串ReadStreamFromResponse(WebResponse响应)
{
使用(Stream responseStream=response.GetResponseStream())
使用(StreamReader sr=新StreamReader(responseStream))
{
//需要返回此响应
字符串strContent=sr.ReadToEnd();
返回strContent;
}
}

您试图在异步响应到达之前返回
结果
,从而破坏异步的目的

您还需要使函数异步


考虑使用
任务
等待

欢迎来到精彩的异步世界!你不能那样做。我知道我不能那样做,但是有什么解决办法吗?我读了很多关于这个的主题,但我不知道为什么我不能使用任务,系统。线程不包含它。关于“等待”,你能再解释一下吗?谢谢。谢谢你的回答和链接。
    public static string GetContentFromURL(string url)
    {
        var task = GetResult(url);
        return task.Result;
    }

    private static Task<string> GetResult(string url)
    {
        url = url.Replace("http://","");
        url = "http://" + url;

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "GET";
        request.UserAgent = "Mozilla/5.0 (Linux; U; Android 4.0.3; de-ch; HTC Sensation Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30";
        Task<WebResponse> task = Task.Factory.FromAsync(
                request.BeginGetResponse, asyncResult => request.EndGetResponse(asyncResult), (object)null
            );

        return task.ContinueWith(t => ReadStreamFromResponse(t.Result));
    }

    private static string ReadStreamFromResponse(WebResponse response)
    {
        using (Stream responseStream = response.GetResponseStream())
        using (StreamReader sr = new StreamReader(responseStream))
        {
            //Need to return this response 
            string strContent = sr.ReadToEnd();
            return strContent;
        }
    }