-1

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?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Where did `sr` come from here? We're not psychic and the details matter. – Marc Gravell Feb 25 '22 at 20:07
  • 3
    **Get rid of the try/catch**. There is an exception being thrown that will tell you exactly what the problem is, and the try/catch is preventing you from seeing that information. (You can always put it back later once you understand how to avoid this). – Joel Coehoorn Feb 25 '22 at 20:10
  • sr is from StreamReader sr = new StreamReader(client.GetStream()); – menemjeff Feb 25 '22 at 20:49
  • I took out the try catch statements and it returned 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.. and so on – menemjeff Feb 25 '22 at 20:51
  • @menemjeff Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then check the [help/on-topic] to see which questions are on-topic on this site. – Progman Feb 25 '22 at 22:23
  • `"The connected party did not respond."` What does that tell you? – Joel Coehoorn Feb 26 '22 at 04:47

1 Answers1

0

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"

Stefan Wuebbe
  • 2,109
  • 5
  • 17
  • 28