我正在使用最新版本的Monodroid4.04,并尝试使用HttpWebRequest连接设备。我遇到的问题是GetResponse抛出了一个webexception (如下所示)。在调试器中,当我设置断点时,我可以看到webRequest.HaveResponse ==为真,但异常中的响应为空。
当它报告有响应时,为什么我无法访问响应?
我使用的代码如下:
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发布于 2012-03-02 05:10:59
我只想说,我最终通过使用套接字解决了这个问题。考虑到数据的性质,这可能是一种更好的方法。
https://stackoverflow.com/questions/9487889
复制相似问题