2013-10-17 36 views
2

當我使用麻煩在C#中使用的GetResponse用於Windows Phone的

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; 

我錯過了什麼? 或者我該怎麼做才能正常工作!

+0

可能重複http://stackoverflow.com/questions/5971866/cant-find-httpwebrequest -getresponse功能於WP7項目) – JMK

回答

6

在windows phone中,您需要執行每個操作,可能需要更長的時間爲50ms異步。由於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); 
     } 
    } 
} 
的[找不到HttpWebRequest.GetResponse()在WP7項目(