简体   繁体   English

处理远程服务器返回错误:(500)内部服务器错误

[英]Handling The remote server returned an error: (500) Internal Server Error

I have a custom HTTP class I made for my application. 我有一个为应用程序制作的自定义HTTP类。 It's just basic, the application is fairly simple. 它只是基本的,应用程序非常简单。 Anyway, sometimes the post method will return the following error: 无论如何,有时post方法将返回以下错误:

Handling The remote server returned an error: (500) Internal Server Error.

My method is 我的方法是

public String post(String url, String postData)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.UserAgent = this.userAgent;
        request.KeepAlive = false;
        request.ProtocolVersion = HttpVersion.Version10;
        request.CookieContainer = this.cookieJar;

        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        request.ContentLength = byteArray.Length;

        Stream stream = request.GetRequestStream();
        stream.Write(byteArray, 0, byteArray.Length);

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);

        String source = reader.ReadToEnd();

        return source;
    }

I'm sending a request to update my Facebook status. 我正在发送更新我的Facebook状态的请求。 Most of the time it works, the odd occasion it doesn't and throws an exception. 在大多数情况下,它起作用,在奇怪的情况下它不起作用,并引发异常。

What's the best way to catch this exception so I can retry the request? 捕获此异常以便重试请求的最佳方法是什么?

It may not be wise to repeat the request. 重复该请求可能不明智。 There are instances where a request fails to return but the receiving server may have processed it, as a result, a retry would result in duplication. 在某些情况下,请求无法返回,但是接收服务器可能已经处理了该请求,结果,重试将导致重复。

It would be better if you raised an error response from this post method - By raising a specific exception - then it is the responsibility of the calling method on deciding what to do. 如果从此post方法引发错误响应(通过引发特定异常)会更好,那么调用方法负责决定要做什么。

It may choose to repeat the request or ignore it or try to validate it was received before attempting to send again. 它可以选择重复请求,或者忽略它,或者在再次发送之前尝试验证它是否已收到。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mandrill : 远程服务器返回错误:(500) 内部服务器错误 - Mandrill : The remote server returned an error: (500) internal server error 远程服务器返回错误:(500)网站上的内部服务器错误 - The remote server returned an error: (500) Internal Server Error on website 未处理WEBException:远程服务器返回错误:(500)内部服务器错误 - WEBException was unhandled: The remote server returned an error: (500) Internal Server Error .ashx-远程服务器返回错误:(500)内部服务器错误 - .ashx - The remote server returned an error: (500) Internal Server Error HttpWebRequest的。 远程服务器返回错误:(500)内部服务器错误 - HttpWebRequest. The remote server returned an error: (500) Internal Server Error webclient远程服务器返回错误:(500)内部服务器错误 - webclient The remote server returned an error: (500) Internal Server Error 远程服务器返回错误:(500)内部服务器错误Web服务 - The remote server returned an error: (500) Internal Server Error Web service 远程服务器返回错误:(500)Internal Server Error。 - The remote server returned an error: (500) Internal Server Error. 远程服务器返回错误:(500)System.Net.HttpWebRequest.GetResponse()上的服务器上的内部服务器错误 - The remote server returned an error: (500) Internal Server Error on server at System.Net.HttpWebRequest.GetResponse() 使用c#将图像发布到Web服务器(远程服务器返回错误(500)内部服务器错误) - Post Image to Web Server in c# (The remote Server returned an error (500) Internal Server Error)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM