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?