2

当我使用

 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

我收到一条错误消息,指出

'System.Net.HttpWebRequest' does not contain a definition for 'GetResponse' and no   extension method 'GetResponse' accepting a first argument of type 'System.Net.HttpWebRequest' could be found (are you missing a using directive or an assembly reference? 

我添加了以下参考,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using PhoneApp23.Resources;
using System.IO;
using System.Text;
using System.Threading;

我错过了什么吗??或者我应该怎么做才能使其正常工作!

4

1 回答 1

6

在 Windows Phone 中,您需要执行每一个可能需要 50 毫秒异步时间的操作。由于 webrequest 可能需要更长的时间,微软从类中删除了同步方法。相反,您需要使用异步方法:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new System.Uri("http://www.google.com"));
request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request); 

private void ReadWebRequestCallback(IAsyncResult callbackResult)
{
    HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
    using(HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult))
    {
        using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
        {
            string results = httpwebStreamReader.ReadToEnd();
            //execute UI stuff on UI thread.
            Dispatcher.BeginInvoke(() => TextBlockResults.Text = results);
        }
    }
} 
于 2013-10-17T09:02:40.273 回答