gpt4 book ai didi

c# - 获取 WebException 错误中的错误编号

转载 作者:可可西里 更新时间:2023-11-01 07:51:21 24 4
gpt4 key购买 nike

如何获取WebException错误中的错误号?

try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("site");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
int i = stream.ReadByte();
}
catch (WebException e)
{
//How To Get Error number in WebException Error?
}

最佳答案

您需要运行测试以确保它是一个ProtocolError:

if (e.Status == WebExceptionStatus.ProtocolError) 
{
Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
}

关于c# - 获取 WebException 错误中的错误编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7440922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com