2011-05-01 69 views
1

我在WP7應用程序中遇到了基本的HttpWebRequest問題。一旦我調用BeginGetRequestStream,它就開始出錯了。回調方法然後接收IAsyncResult而沒有AsyncWaitHandleWp7上的HttpWebRequest AsyncWaitHandle有System.NotSupportedException

AsyncWaitHandle =「ar.AsyncWaitHandle」投擲類型的異常的「System.NotSupportedException」

我創建低於樣品產生錯誤。爲了創建這個,我剛剛採用了示例數據透視應用程序,並嘗試將一些數據發佈到MainViewModel的構造函數中的服務器上。

public MainViewModel() 
    { 
     this.Items = new ObservableCollection<ItemViewModel>(); 

     HttpWebRequest webRequest = WebRequest.CreateHttp("http://www.google.co.uk"); 

     webRequest.Method = "POST"; 
     webRequest.ContentType = "application/x-www-form-urlencoded"; 

     webRequest.BeginGetRequestStream(BeginGetRequestStreamCallBack, webRequest); 
    } 

    private void BeginGetRequestStreamCallBack(IAsyncResult ar) 
    { 
     HttpWebRequest webRequest = (HttpWebRequest) ar.AsyncState; 

     Stream requestStream = webRequest.EndGetRequestStream(ar); 

     using(StreamWriter sw = new StreamWriter(requestStream)) 
     { 
      sw.Write("{ test: \"test\" }"); 
     } 

     webRequest.BeginGetResponse(BeginGetResponseCallback, webRequest); 
    } 

    private void BeginGetResponseCallback(IAsyncResult ar) 
    { 
     HttpWebRequest webRequest = (HttpWebRequest)ar.AsyncState; 

     WebResponse webResponse = webRequest.EndGetResponse(ar); 

     Stream responseStream = webResponse.GetResponseStream(); 

     string whatCameBack = new StreamReader(responseStream).ReadToEnd(); 
    } 

任何幫助將一如既往,不勝感激。

我已經深入瞭解了這一點,我似乎無法使用BeginGetRequestStream發佈數據。

public MainViewModel() 
    { 
     this.Items = new ObservableCollection<ItemViewModel>(); 

     HttpWebRequest webRequest = WebRequest.CreateHttp("http://www.google.co.uk"); 

     //webRequest.Method = "POST"; 
     //webRequest.ContentType = "application/x-www-form-urlencoded"; 

     //webRequest.BeginGetRequestStream(BeginGetRequestStreamCallBack, webRequest); 
     webRequest.BeginGetResponse(BeginGetResponseCallback, webRequest); 
    } 

    private void BeginGetRequestStreamCallBack(IAsyncResult ar) 
    { 
     HttpWebRequest webRequest = (HttpWebRequest) ar.AsyncState; 

     Stream requestStream = webRequest.EndGetRequestStream(ar); 

     using(StreamWriter sw = new StreamWriter(requestStream)) 
     { 
      sw.Write("{ test: \"test\" }"); 
     } 

     webRequest.BeginGetResponse(BeginGetResponseCallback, webRequest); 
    } 

    private void BeginGetResponseCallback(IAsyncResult ar) 
    { 
     HttpWebRequest webRequest = (HttpWebRequest)ar.AsyncState; 

     WebResponse webResponse = webRequest.EndGetResponse(ar); 

     Stream responseStream = webResponse.GetResponseStream(); 

     string whatCameBack = new StreamReader(responseStream).ReadToEnd(); 
    } 

這工作正常,但它不是我想要的。

任何想法?

回答

0

我是一個頑皮的男孩,而不是正確關閉流。

using(Stream requestStream = webRequest.EndGetRequestStream(ar)) 
{} 

一切都很好。