我有一个为我的应用程序制作的自定义 HTTP 类。这只是基本的,应用程序相当简单。无论如何,有时 post 方法会返回以下错误:
Handling The remote server returned an error: (500) Internal Server Error.
我的方法是
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;
}
我正在发送更新我的 Facebook 状态的请求。大多数情况下它都可以正常工作,但在奇怪的情况下它不会并引发异常。
捕获此异常以便我可以重试请求的最佳方法是什么?