2012-11-17 206 views
0

我有以下類(我從網上的例子中看到,我修改的唯一的東西是我使用的是IP地址和端口,而不是域名):在Windows Phone 7上的WebRequest

public class ConnectionManager 
{ 
    private static ManualResetEvent allDone = new ManualResetEvent(false); 
    private string message = "foobar"; 

    public Action MessageSent; 
    public Action<string> MessageReceived; 
    public void SendMessage() 
    { 
      // Create a new HttpWebRequest object. 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://10.1.91.48:3330/"); 

     request.ContentType = "application/json; charset=utf-8"; 
     request.Accept = "application/json"; 
     // Set the Method property to 'POST' to post data to the URI. 
     request.Method = "POST"; 

     // start the asynchronous operation 
     request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request); 
     MessageSent(); 
     // Keep the main thread from continuing while the asynchronous 
     // operation completes. A real world application 
     // could do something useful such as updating its user interface. 
     allDone.WaitOne(); 
    } 
    private void GetRequestStreamCallback(IAsyncResult asynchronousResult) 
    { 
     HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; 

     // End the operation 
     Stream postStream = request.EndGetRequestStream(asynchronousResult); 


     // Convert the string into a byte array. 
     byte[] byteArray = Encoding.UTF8.GetBytes(message); 

     // Write to the request stream. 
     postStream.Write(byteArray, 0, byteArray.Length); 
     postStream.Close(); 

     // Start the asynchronous operation to get the response 
     request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request); 
    } 

    private void GetResponseCallback(IAsyncResult asynchronousResult) 
    { 
     HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; 

     // End the operation 
     HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult); 
     Stream streamResponse = response.GetResponseStream(); 
     StreamReader streamRead = new StreamReader(streamResponse); 
     string responseString = streamRead.ReadToEnd(); 
     Console.WriteLine(responseString); 
     // Close the stream object 
     streamResponse.Close(); 
     streamRead.Close(); 

     // Release the HttpWebResponse 
     response.Close(); 
     allDone.Set(); 

     MessageReceived(responseString); 
    } 
} 

上面的代碼未能發送消息。如果我一步,當我在裏面GetRequestStreamCallback我可以在裏面看到的IAsyncResult以下錯誤:

AsyncWaitHandle = 'asynchronousResult.AsyncWaitHandle' threw an exception of type 'System.NotSupportedException'

我在做什麼錯?我怎樣才能解決這個問題?

回答

1

我結束了使用Web客戶端:

WebClient wc = new WebClient(); 
wc.DownloadStringCompleted += ReadServerResponse; 
wc.DownloadStringAsync(new Uri(url)); 
1

雖然它可能不是解決你的問題,你需要進入放置您IDisposable對象的習慣成使用塊,以確保他們得到清理即使例外發生:

private void GetRequestStreamCallback(IAsyncResult asynchronousResult) 
{ 
    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; 

    // End the operation 
    using (Stream postStream = request.EndGetRequestStream(asynchronousResult)) 
    { 
     byte[] byteArray = Encoding.UTF8.GetBytes(message); 

     // Write to the request stream. 
     postStream.Write(byteArray, 0, byteArray.Length); 
     postStream.Close(); 
    } 

    // Start the asynchronous operation to get the response 
    request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request); 
} 

private void GetResponseCallback(IAsyncResult asynchronousResult) 
{ 
    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; 

    // End the operation 
    string responseString; 
    using (HttpWebResponse response = (HttpWebResponse) request.EndGetResponse(asynchronousResult)) 
    { 
     using (Stream streamResponse = response.GetResponseStream()) 
     { 
      using (StreamReader streamRead = new StreamReader(streamResponse)) 
      { 
       responseString = streamRead.ReadToEnd(); 
       Console.WriteLine(responseString); 
      } 
     } 
    } 
    allDone.Set(); 

    MessageReceived(responseString); 
}