Is there a way to tell the server to abort the request(s) that timed out on the client so that these artificial delays do not happen? I've really had a hard time finding anything online about this. Most search results give me something about closing the request on the client only or SQL connections. I have tried closing the web request (not just the stream) and as you can see in the code below, closing the requests in the exceptions (although I'd think that would happen naturally when the function exited). I'm not really sure what else I could try and do.
If this is not possible, disconnecting my network connection and then reconnecting seems to fix the issue without restarting the program. Would there a way to tell my network adapter to close all connections with the site?
Expand|Select|Wrap|Line Numbers
- private String SendRequestTo(String method, Byte[] requestBytes, Uri destination, String refer, NetworkCredential credentials, ThreadSource source)
- {
- HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(destination);
- webRequest.Method = method;
- webRequest.Accept = "*/*";
- webRequest.AllowAutoRedirect = false;
- webRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12";
- webRequest.CookieContainer = new CookieContainer();
- webRequest.ContentLength = requestBytes.Length;
- webRequest.ContentType = "application/x-www-form-urlencoded";
- webRequest.PreAuthenticate = true;
- webRequest.Referer = refer;
- if (source == ThreadSource.WebCT || source == ThreadSource.Directory || source == ThreadSource.DLS || source == ThreadSource.VistaKeepAlive)
- webRequest.Timeout = 10000;
- if (source == ThreadSource.Vista)
- webRequest.Timeout = 30000;
- webRequest.Credentials = credentials;
- webRequest.CookieContainer.Add(cookies.GetCookies(destination));
- Stream reqStream = null;
- StreamReader stream = null;
- HttpWebResponse webResponse = null;
- try
- {
- if (method == "post")
- {
- reqStream = webRequest.GetRequestStream();
- reqStream.Write(requestBytes, 0, requestBytes.Length);
- reqStream.Close();
- }
- webResponse = (HttpWebResponse)webRequest.GetResponse();
- if (webRequest.HaveResponse)
- {
- foreach (Cookie retCookie in webResponse.Cookies)
- {
- bool cookieFound = false;
- foreach (Cookie oldCookie in cookies.GetCookies(destination))
- {
- if (retCookie.Name.Equals(oldCookie.Name))
- {
- oldCookie.Value = retCookie.Value;
- cookieFound = true;
- }
- }
- if (!cookieFound)
- cookies.Add(retCookie);
- }
- if ((webResponse.StatusCode == HttpStatusCode.Found) || (webResponse.StatusCode == HttpStatusCode.Redirect) || (webResponse.StatusCode == HttpStatusCode.Moved) || (webResponse.StatusCode == HttpStatusCode.MovedPermanently))
- {
- WebHeaderCollection headers = webResponse.Headers;
- return SendRequestTo(method, requestBytes, new Uri(headers["location"]), refer, credentials, source);
- }
- stream = new StreamReader(webResponse.GetResponseStream());
- String responseString = stream.ReadToEnd();
- stream.Close();
- return responseString;
- }
- }
- catch (WebException e)
- {
- try
- {
- reqStream.Close();
- }
- catch (Exception)
- {
- }
- try
- {
- stream.Close();
- }
- catch (Exception)
- {
- }
- try
- {
- webResponse.Close();
- }
- catch (Exception)
- {
- }
- throw new Exception("Exception occured while sending request.", e);
- }
- throw new Exception("No response received from host.");
- }