简体   繁体   中英

C# HttpWebResponse, WebException

So, I am fairly new to C#, but I have worked with other languages. Currently I am coding a custom MineCraft launcher. I am using the Yggdrasil Authentication Documentation to help me do this. It says: If a request was successful the server will respond with:

  • Status code 200
  • A JSON-encoded dictionary according to the specifications below

If however a request fails, the server will respond with:

  • An appropriate, non-200 HTTP status code
  • A JSON-encoded dictionary following this format:

I have solved the first part, but the second part is where the problem is!

HttpWebResponse httpResponse = null;
try
{
    httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        string result = streamReader.ReadToEnd();
        Console.WriteLine(result);
    }
}
catch (WebException e)
{
    Console.WriteLine(e.ToString());
}

As you see, if a WebException occurs, such as for example I receive a 403 Forbidden error, I can't read the contents. Ijust get a NullReferenceException.

So, the question is: How to get the WebResponse if the HttpWebRequest fails?

Please check the below method to get the status code.

     HttpWebResponse httpResponse = null;
    try
    {
        httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            string result = streamReader.ReadToEnd();
            Console.WriteLine(result);
        }
    }
catch (WebException ex)
{
    if (ex.Status == WebExceptionStatus.ProtocolError)
    {
        var response = ex.Response as HttpWebResponse;
        if (response != null)
        {
            Console.WriteLine("HTTP Status Code: " + (int)response.StatusCode);
        }
        else
        {
            // no http status code available
        }
    }
    else
    {
        // no http status code available
    }
}

You can still read response content by using WebException.Response property:

HttpWebResponse httpResponse = null;
try
{
    httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        string result = streamReader.ReadToEnd();
        Console.WriteLine(result);
    }
}
catch (WebException e)
{
   Console.WriteLine(e.ToString());

   string responseText;
   using (var reader = new StreamReader(webException.Response.GetResponseStream()))
   {
       responseText = reader.ReadToEnd();
   }

   Console.WriteLine("WebException caught. Response text is {0}", responseText);
}

you can try something like this:

        var response = e.Response as HttpWebResponse;
         if (response!=null && response.StatusCode != HttpStatusCode.OK)
        {
            Console.WriteLine("HTTP Error Code is: " + (int)response.StatusCode);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM