1

我编写了一个从网页获取一些数据的 .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;

上面的代码被包装在一个方法中,并被多次调用以获取要获取的数据列表。第一次调用总是成功的。在第 2 次呼叫之后,UploadString()将阻塞 30 秒,然后出现上述异常。我通过观察控制台输出注意到了这一点。如果我让它循环运行,几次尝试后它会成功,然后再次失败。

我有另一种具有类似代码的方法,但OpenRead()改为调用并返回内容。这在我的 VPS 上运行没有任何问题。还有 2 个 PHP5 Web 应用程序托管在 VPS 上,运行没有任何问题。

由于我的 VPS 不支持 X11,而且我不知道如何使用 VS2010 远程调试代码(或者我应该使用 MonoDevelop)。我希望以前遇到过类似问题的人对此有答案,或者有一些调试此类似设置的指南。

有一个类似的问题,WebClient“错误获取响应流 ,但答案没有帮助。谢谢

编辑:我突然想到我可以在我的开发机器上运行 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 并使用它运行之后,我得到了一个不同的异常。不确定它是否与分块传输编码有关,但同样,当我在 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 上

4

1 回答 1

2

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

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

于 2013-02-05T10:23:42.460 回答