Webclient.UploadString在后续调用中获取System.Net.WebException:Receiv

本文关键字:Net System WebException Receiv 获取 UploadString 调用 Webclient | 更新日期: 2023-09-27 17:57:26

我编写了一个.net 3.5控制台应用程序,可以从网页中获取一些数据。当在我的Windows7开发机器上调试和运行时,它工作得很好。

但是,当它在我托管的运行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,一旦完成就处理Web客户端)

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在获取响应流时出错,但答案没有帮助。感谢

编辑:我突然想到我可以在我的开发机器上运行MonoDevelop并进行调试。现在就这么做。

编辑:试图在Windows上使用MonoDevelop+Mono 2.6.7进行调试会产生一系列不同的问题,MySQL。数据无法连接到服务器和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传输编码有关,但同样,当我在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)

编辑:如果我使用WebClient,我会得到ProtocolViolationException,但我得到ReceiveFailure,这与之前使用HttpWebRequest的情况相同,都在Mono 2.10.8 上

Webclient.UploadString在后续调用中获取System.Net.WebException:Receiv

当您提到您正在使用Mono 2.6.7时,我停止了阅读。这个版本已经有两年多的历史了。

在试图找到罪魁祸首或添加任何解决方法之前,您应该首先检查问题是否仍然出现在Mono 3.2中,它是最后一个稳定的版本(有点像LTS)。