我们有兴趣捕获status code
+ status code description
,它描述了在用户遇到连接错误时捕获异常时的错误。此方法在消息框中打印出该事件中的 Web 异常(此处未显示)。
为什么它不显示状态代码?
internal static List<string> getHeaders(string url, string postdata)
{
List<string> headers = new List<string>();
try
{
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] byte1 = encoding.GetBytes(postdata);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 20000;
request.AllowAutoRedirect = false;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byte1.Length;
request.CookieContainer = new CookieContainer();
request.Credentials = CredentialCache.DefaultCredentials;
Stream newstream = request.GetRequestStream();
newstream.Write(byte1, 0, byte1.Length);
WebResponse response = request.GetResponse();
HttpWebResponse httpresponse = (HttpWebResponse)response;
headers.Add("Status Code: " + (int)httpresponse.StatusCode);
headers.Add("Status Desc: " + httpresponse.StatusDescription);
foreach (string key in response.Headers.Keys)
{
if (!key.ToString().Equals("Location"))
{
var value = httpresponse.Headers[key];
headers.Add(key + ": " + value);
}
}
return headers;
}
catch (Exception ex)
{
headers.Add("ERROR MESSAGE: " + ex.Message);
return headers;
}
}
这是对这个线程的引用,它在try-catching
标头时向我们显示状态代码和状态代码描述。
没
关系,代码似乎工作正常。但是,返回的错误the remote name could not be resolved
不会返回状态代码。