Reputation: 12599
I have a custom HTTP class I made for my application. It's just basic, the application is fairly simple. Anyway, sometimes the post method will return the following error:
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. 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?
Upvotes: 1
Views: 6642
Reputation: 19407
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.
It may choose to repeat the request or ignore it or try to validate it was received before attempting to send again.
Upvotes: 2