我编写了一个.net 3.5控制台应用程序,用于从网页中获取一些数据。在我的Windows 7开发机器上进行调试和运行时,它运行正常。
但是当它在运行Debian 6.0.5和Mono 2.6.7的托管VPS上运行时,会在第二次或稍后调用UploadString()
时抛出以下异常
System.Net.WebException: Error getting response stream (ReadDone2): ReceiveFailure ---> System.Exception: at System.Net.WebConnection.HandleError(WebExceptionStatus st, System.Exception e, System.String where)
at System.Net.WebConnection.ReadDone(IAsyncResult result)
at System.Net.Sockets.Socket+SocketAsyncResult.Complete()
at System.Net.Sockets.Socket+Worker.Receive()
at System.Net.WebConnection.HandleError (WebExceptionStatus st, System.Exception e, System.String where) [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0
at System.Net.HttpWebRequest.GetResponse () [0x00000] in <filename unknown>:0
at System.Net.WebClient.GetWebResponse (System.Net.WebRequest request) [0x00000] in <filename unknown>:0
at System.Net.WebClient.ReadAll (System.Net.WebRequest request, System.Object userToken) [0x00000] in <filename unknown>:0
at System.Net.WebClient.UploadDataCore (System.Uri address, System.String method, System.Byte[] data, System.Object userToken) [0x00000] in <filename unknown>:0
at System.Net.WebClient.UploadData (System.Uri address, System.String method, System.Byte[] data) [0x00000] in <filename unknown>:0
我的代码非常直接(我有一个try / catch / finally,一旦完成就处理了webclient)
client = new WebClient();
client.Proxy = null;
client.Credentials = new NetworkCredential("xxx", "xxx");
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string postString = "";
foreach (KeyValuePair<string, string> pair in postData)
{
postString += pair.Key + "=" + pair.Value + "&";
}
string content = client.UploadString(url, postString);
return content;
上面的代码包含在一个方法中,并且被多次调用以获取要提取的数据列表。第一次通话总是成功的。在第二次通话时,UploadString()
将阻止30秒,然后是上述例外。通过观察控制台输出我注意到了这一点。如果我让它在循环中运行,经过几次尝试后它会成功,然后再次失败。
我有另一种方法,它有类似的代码,但是调用OpenRead()
代替并返回内容。这在我的VPS上没有任何问题。另外,在VPS上托管了2个PHP5 Web应用程序,运行时没有任何问题。
由于我的VPS不支持X11,我不知道如何使用VS2010远程调试代码(或者我应该使用MonoDevelop)。我希望之前遇到过类似问题的人有一个答案,或者有一些指导来调试这个类似的设置。
有一个类似的问题,WebClient “error getting response stream,但答案没有帮助。 感谢
编辑:我想到我可以在我的开发机器上运行MonoDevelop并进行调试。现在就这样做。 编辑:在Windows上尝试使用MonoDevelop + Mono 2.6.7进行调试会产生一系列问题,MySQL.Data无法连接到服务器,而WebClient会出现超时错误。我使用HttpWebRequest,在VPS上运行但在我的机器上运行时遇到类似的异常
System.Net.WebException: Error getting response stream (ReadDone2): ReceiveFailure ---> System.Exception: at System.Net.WebConnection.HandleError(WebExceptionStatus st, System.Exception e, System.String where)
at System.Net.WebConnection.ReadDone(IAsyncResult result)
at System.Net.Sockets.Socket+SocketAsyncResult.Complete()
at System.Net.Sockets.Socket+Worker.Receive()
at System.Net.WebConnection.HandleError (WebExceptionStatus st, System.Exception e, System.String where) [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0
at System.Net.HttpWebRequest.GetResponse () [0x00000] in <filename unknown>:0
HttpWebRequest实际上是在WebClient中使用的,因此获得相同的异常似乎是合理的。
编辑:能够编译Mono 2.10.8并运行它后,我得到了一个不同的例外。不确定它是否与Chunked transfer encoding有关,但在Windows中调试时再没有这样的错误。System.Net.WebException: An error occurred performing a WebClient request. ---> System.Net.ProtocolViolationException: The number of bytes to be written is greater than the specified ContentLength.
at System.Net.WebConnectionStream.CheckWriteOverflow (Int64 contentLength, Int64 totalWritten, Int64 size) [0x00000] in <filename unknown>:0
at System.Net.WebConnectionStream.BeginWrite (System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback cb, System.Object state) [0x00000] in <filename unknown>:0
at System.Net.WebConnectionStream.Write (System.Byte[] buffer, Int32 offset, Int32 size) [0x00000] in <filename unknown>:0
at System.Net.WebClient.UploadDataCore (System.Uri address, System.String method, System.Byte[] data, System.Object userToken) [0x00000] in <filename unknown>:0
at System.Net.WebClient.UploadData (System.Uri address, System.String method, System.Byte[] data) [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at System.Net.WebClient.UploadData (System.Uri address, System.String method, System.Byte[] data) [0x00000] in <filename unknown>:0
at System.Net.WebClient.UploadData (System.Uri address, System.Byte[] data) [0x00000] in <filename unknown>:0
at System.Net.WebClient.UploadData (System.String address, System.Byte[] data) [0x00000] in <filename unknown>:0
at System.Net.WebClient.UploadString (System.String address, System.String data) [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) System.Net.WebClient:UploadString (string,string)
编辑:如果我使用ProtocolViolationException
,我会得到WebClient
,但我得到ReceiveFailure
,这与我之前使用HttpWebRequest时的异常相同,所有这些都在Mono上2.10.8
答案 0 :(得分:2)
当你提到你正在使用Mono 2.6.7时,我停止阅读。这个版本已经有两年多了。
在尝试找到罪魁祸首或添加任何变通方法之前,首先应检查问题是否仍然出现在Mono 3.2中,这是最后一个稳定版本(类似于LTS)。