Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WebClient:忽略HTTP 500_C#_.net_Webclient - Fatal编程技术网

C# WebClient:忽略HTTP 500

C# WebClient:忽略HTTP 500,c#,.net,webclient,C#,.net,Webclient,我正在编写一个程序,从服务器检索一些数据,对其执行一些操作,并将输出保存到csv文件中。我的问题是服务器(我不负责)总是返回HTTP 500内部服务器错误。我已经和负责这项工作的团队谈过了,虽然他们知道这个bug,但他们说它的影响不足以让他们解决 有没有一种方法可以让我在代码中忽略此响应,仍然可以获取数据?如果您使用的是HttpWebRequest/response,这应该可以让您开始: response = null; try { HttpWebRequest request = (

我正在编写一个程序,从服务器检索一些数据,对其执行一些操作,并将输出保存到csv文件中。我的问题是服务器(我不负责)总是返回HTTP 500内部服务器错误。我已经和负责这项工作的团队谈过了,虽然他们知道这个bug,但他们说它的影响不足以让他们解决


有没有一种方法可以让我在代码中忽略此响应,仍然可以获取数据?

如果您使用的是HttpWebRequest/response,这应该可以让您开始:

response = null;

try
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("<url>");

    response = (HttpWebResponse)request.GetResponse();

    //no error
}
catch (WebException e)
{
    if (e.Status == WebExceptionStatus.ProtocolError)
    {
        response = (HttpWebResponse)e.Response;

        if((int)response.StatusCode == 500)
        {
            using (StreamReader sr = new StreamReader(response.GetResponseStream()))
            {
                var result = sr.ReadToEnd();
            }
        }
    }
}
response=null;
尝试
{
HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(“”);
response=(HttpWebResponse)request.GetResponse();
//无误
}
捕获(WebE例外)
{
if(e.Status==WebExceptionStatus.ProtocolError)
{
响应=(HttpWebResponse)e.response;
如果((int)response.StatusCode==500)
{
使用(StreamReader sr=newstreamreader(response.GetResponseStream()))
{
var result=sr.ReadToEnd();
}
}
}
}

服务器是否返回数据且仅状态代码错误,或者响应正文是否为错误消息(例如ASP.NET错误)?请求是否在web浏览器中工作?返回的状态码是什么?浏览器请求和您的请求之间存在哪些差异?使用Wireshark或Fiddler查看HTTP流量。服务器会返回数据,是的。只有响应代码是错误的。它在网络浏览器中工作。谢谢Chad,成功了。我没有使用HttpWebRequest,我只是使用WebClient。但这是可行的。对于任何感兴趣的人,如果您使用WebClient,也可以使用此方法。只需捕获WebException并如上所述处理它。