I have this code:
public static string Connect(string Uri)
{
try
{
HttpWebRequest connection = WebRequest.Create(requestURI) as HttpWebRequest;
connection.Method = "GET";
string response;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
using (var responseStream = new StreamReader(response.GetResponseStream()))
{
responseText = responseStream.ReadToEnd();
}
return response;
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
If the API return 200
http status the response
variable is returned correctly, instead if I have client error 400
or 500
the code fall in exception. I want manage this exception in the try
instead fall in the Console.WriteLine
, there is a change for do this?