我想使用下面的代码返回页面的HTML结果,即使我得到403响应代码,例如,如果它抛出异常。
这是我当前的代码,正如您所看到的,我想在抛出异常时返回页面的HTML结果。
public string authenticate(string user, string pass)
{
try
{
var request = (HttpWebRequest)WebRequest.Create("https://authserver.mojang.com/authenticate");
request.ContentType = "application/json";
request.Method = "POST";
using (var writer = new StreamWriter(request.GetRequestStream()))
{
string json = "{\"agent\":{\"name\":\"Minecraft\",\"version\":1},\"username\":\"" + user + "\",\"password\":\"" + pass + "\",\"clientToken\":\"6c9d237d-8fbf-44ef-b46b-0b8a854bf391\"}";
writer.Write(json);
writer.Flush();
writer.Close();
var response = (HttpWebResponse)request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
return reader.ReadToEnd(); // the html result
}
}
}
catch (Exception)
{
return "the html result";
}
}
感谢您的帮助。