2012-02-28 68 views
0

我正在使用最新版本的Monodroid,4.04,並試圖使用HttpWebRequest連接到設備。我遇到的問題是GetResponse正在拋出webexception(請參閱下文)。 在調試器中,當我設置一個斷點時,我可以看到webRequest.HaveResponse == true,但異常中的響應爲空。HttpWebRequest.GetResponse()拋出WebException

爲什麼當它報告有響應時我無法得到響應?

我使用的代碼:

string responseFromServer; 
     ServicePointManager.CertificatePolicy = new TestCertificatePolicy(); 

     HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://192.168.1.1/login.cgi"); 
     webRequest.KeepAlive = true; 
     webRequest.Method = "POST"; 
     webRequest.ContentType = "application/x-www-form-urlencoded"; 
     webRequest.ContentLength = byteArray.Length; 
     webRequest.ReadWriteTimeout = 10000; 

     // Get the request stream. 
     Stream dataStreamIn = webRequest.GetRequestStream(); 

     dataStreamIn.Write(byteArray, 0, byteArray.Length); 
     dataStreamIn.Close(); 

     try 
     { 
      WebResponse response = webRequest.GetResponse(); <-- throws webexception 
      Stream dataStream = response.GetResponseStream(); 
      StreamReader reader = new StreamReader(dataStream); 

      responseFromServer = reader.ReadToEnd(); 
      Console.WriteLine(responseFromServer); 
      reader.Close(); 
      dataStream.Close(); 
      response.Close(); 

      Console.WriteLine("Done"); 

     } 
     catch (WebException we) 
     { 
      Console.WriteLine(we); 
      throw; 
     } 

這裏是個例外:

System.Net.WebException: Error getting response stream (ReadDone2): ReceiveFailure ---> System.Exception: at System.Net.WebConnection.ReadDone(IAsyncResult result) 
    at System.Net.WebConnection.HandleError (WebExceptionStatus st, System.Exception e, System.String where) [0x0003a] in /home/jon/Development/xamarin/mono/mcs/class/System/System.Net/WebConnection.cs:399 
    --- End of inner exception stack trace --- 
    at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x0005e] in /home/jon/Development/xamarin/mono/mcs/class/System/System.Net/HttpWebRequest.cs:828 
    at System.Net.HttpWebRequest.GetResponse() [0x0000e] in /home/jon/Development/xamarin/mono/mcs/class/System/System.Net/HttpWebRequest.cs:836 
    at MonoDroidTest.Activity1.GetStreamPage (System.String url) [0x000c2] in C:\Users\dave\Documents\Visual Studio 2010\Projects\MonoDroidTest\MonoDroidTest\Activity1.cs:277 
+0

是否存在嵌套異常? – 2012-02-28 18:56:29

+0

不,不存在 – 2012-02-28 19:10:35

+0

您是否可以使用瀏覽器應用程序訪問該URL? – 2012-02-29 04:31:33

回答

0

只想說,我結束了使用插座解決這個問題。由於數據的性質,這可能是更好的方法。

相關問題