HTTP 1.1 协议从服务器读取行错误
我正在通过 HTTP 1.1 向服务器发送消息。所有内容都正确发送到我选择的服务器或网站,但是当我收到服务器/网站的响应并且我的 sr.readToEnd() 执行时,它终止。
我知道我发送的消息是正确的,但我正在尝试执行 try-catch 语句,如果它再次终止,它将尝试以另一种方式读取。我不知道如何做到这一点,有人建议我可以使用内容长度(除非我也不知道如何做到这一点)。
这是我到目前为止所得到的:
try
{ //Read server message
String response = sr.ReadToEnd();
}
catch
{ //If terminate occurs, read a different way
}
如果我删除 try/catch 块,我会看到以下内容:
未处理的异常:System.IO.IOException:无法从传输连接读取数据:连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机未能响应.
---> System.Net.Sockets.SocketException:连接尝试失败,因为连接方在一段时间后没有正确响应。
我知道我提供的内容非常简短,但是有任何方法可以解决我描述的此类问题吗?
I am sending a message to a server through HTTP 1.1. Everything sends correctly to the server or website I have chosen, but when I receive the response from the server/website and my sr.readToEnd()
executes, it terminates.
I know that the message I have sent is correct, but I am trying to do a try-catch statement were if it terminates again, it will try to read another way. I am not sure how to do this and I was advised that I could use content-length (except I do not know how to do that either).
Here is what I have so far:
try
{ //Read server message
String response = sr.ReadToEnd();
}
catch
{ //If terminate occurs, read a different way
}
If I remove the try/catch blocks I see this:
Unhandled Exception: System.IO.IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time..
I know it's pretty brief what I provided, but any methods to tackle this sort of problem I described?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说一般来说你可以这样做(正如@joel推荐的那样。
或者尝试一下
catch when
() 是否适合您。示例:
另请参阅使用“catch,when”捕获异常
I'd say in general you can do (as @joel recommended.
Or try whether
catch when
() works for you.Example:
See also Catching exceptions with "catch, when"