【发布时间】:2015-04-24 07:20:24
【问题描述】:
所以,我是 C# 的新手,但我使用过其他语言。 目前我正在编写一个自定义的 MineCraft 启动器。 我正在使用Yggdrasil Authentication Documentation 来帮助我做到这一点。 它说: 如果请求成功,服务器将响应:
- 状态码 200
- 符合以下规范的 JSON 编码字典
如果请求失败,服务器将响应:
- 适当的非 200 HTTP 状态代码
- 遵循此格式的 JSON 编码字典:
我已经解决了第一部分,但第二部分是问题所在!
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());
}
如您所见,如果发生 WebException,例如我收到 403 Forbidden 错误,我将无法读取内容。我只是得到一个 NullReferenceException。
那么,问题来了:如果HttpWebRequest失败,如何获取WebResponse?
【问题讨论】:
-
你能打开 fiddler 并实际看到客户端的响应吗?
标签: c# json httpwebrequest httpwebresponse system.net.webexception