HTTP 1.1 协议从服务器读取行错误

发布于 2025-01-10 03:23:35 字数 709 浏览 0 评论 0原文

我正在通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

年华零落成诗 2025-01-17 03:23:35

我想说一般来说你可以这样做(正如@joel推荐的那样。
或者尝试一下 catch when() 是否适合您。
示例:

try
{
    someCode();
}
catch (YourExpectedException ex) when (
    someBooleanExpression 
    || someOtherBooleanExpr
    || thirdBoolean)
{
    /* this part runs when() YourExpectedException occurs *and*
     one of those three hypothetical example expressions is True */
}
catch (YourExpectedException ex)
{
    /* this part runs If YourExpectedException occurs and the previous 
    When() expression is *Not true */
    Whatever();
} /* you can continue catching other expected exceptions 
     and when() combinations here.
     And/Or perhaps say any other exception is unexpected and 
     will be handled by a higher-level:
   */
catch // any other Exception just to re-throw it back 'to-whom-it-may-concern':
    throw;

另请参阅使用“catch,when”捕获异常

I'd say in general you can do (as @joel recommended.
Or try whether catch when() works for you.
Example:

try
{
    someCode();
}
catch (YourExpectedException ex) when (
    someBooleanExpression 
    || someOtherBooleanExpr
    || thirdBoolean)
{
    /* this part runs when() YourExpectedException occurs *and*
     one of those three hypothetical example expressions is True */
}
catch (YourExpectedException ex)
{
    /* this part runs If YourExpectedException occurs and the previous 
    When() expression is *Not true */
    Whatever();
} /* you can continue catching other expected exceptions 
     and when() combinations here.
     And/Or perhaps say any other exception is unexpected and 
     will be handled by a higher-level:
   */
catch // any other Exception just to re-throw it back 'to-whom-it-may-concern':
    throw;

See also Catching exceptions with "catch, when"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文