473,385 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

C# How to instruct a web server to cancel a request?

1
I'm writing an application that simplifies the interface to a few websites I use at my job. One of the web servers has a tendency to cause my program to throw a time out exception for a variety of reasons. This is an issue since this web site uses a persistent connection to keep you logged in. So if the request isn't finished and received by my program, the server keeps that open and after two time outs my program just becomes "hosed" and must be restarted. From my troubleshooting, I've found that after two times outs, the server puts roughly a 20 second delay before it returns a new request to you. After three time outs, this delay becomes roughly a minute. This is longer than my program's time out, but using a real web browser I could access the page, but only after a very long delay.

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
  1.             private String SendRequestTo(String method, Byte[] requestBytes, Uri destination, String refer, NetworkCredential credentials, ThreadSource source)
  2.             {
  3.                 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(destination);
  4.                 webRequest.Method = method;
  5.                 webRequest.Accept = "*/*";
  6.                 webRequest.AllowAutoRedirect = false;
  7.                 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";
  8.                 webRequest.CookieContainer = new CookieContainer();
  9.                 webRequest.ContentLength = requestBytes.Length;
  10.                 webRequest.ContentType = "application/x-www-form-urlencoded";
  11.                 webRequest.PreAuthenticate = true;
  12.                 webRequest.Referer = refer;
  13.                 if (source == ThreadSource.WebCT || source == ThreadSource.Directory || source == ThreadSource.DLS || source == ThreadSource.VistaKeepAlive)
  14.                     webRequest.Timeout = 10000;
  15.  
  16.                 if (source == ThreadSource.Vista)
  17.                     webRequest.Timeout = 30000;
  18.  
  19.                 webRequest.Credentials = credentials;
  20.                 webRequest.CookieContainer.Add(cookies.GetCookies(destination));
  21.  
  22.                 Stream reqStream = null;
  23.                 StreamReader stream = null;
  24.                 HttpWebResponse webResponse = null;
  25.  
  26.                 try
  27.                 {
  28.                     if (method == "post")
  29.                     {
  30.                         reqStream = webRequest.GetRequestStream();
  31.                         reqStream.Write(requestBytes, 0, requestBytes.Length);
  32.                         reqStream.Close();
  33.                     }
  34.  
  35.                     webResponse = (HttpWebResponse)webRequest.GetResponse();
  36.                     if (webRequest.HaveResponse)
  37.                     {
  38.                         foreach (Cookie retCookie in webResponse.Cookies)
  39.                         {
  40.                             bool cookieFound = false;
  41.                             foreach (Cookie oldCookie in cookies.GetCookies(destination))
  42.                             {
  43.                                 if (retCookie.Name.Equals(oldCookie.Name))
  44.                                 {
  45.                                     oldCookie.Value = retCookie.Value;
  46.                                     cookieFound = true;
  47.                                 }
  48.                             }
  49.                             if (!cookieFound)
  50.                                 cookies.Add(retCookie);
  51.                         }
  52.  
  53.                         if ((webResponse.StatusCode == HttpStatusCode.Found) || (webResponse.StatusCode == HttpStatusCode.Redirect) || (webResponse.StatusCode == HttpStatusCode.Moved) || (webResponse.StatusCode == HttpStatusCode.MovedPermanently))
  54.                         {
  55.                             WebHeaderCollection headers = webResponse.Headers;
  56.                             return SendRequestTo(method, requestBytes, new Uri(headers["location"]), refer, credentials, source);
  57.                         }
  58.  
  59.                         stream = new StreamReader(webResponse.GetResponseStream());
  60.                         String responseString = stream.ReadToEnd();
  61.                         stream.Close();
  62.                         return responseString;
  63.                     }
  64.                 }
  65.                 catch (WebException e)
  66.                 {
  67.                     try
  68.                     {
  69.                         reqStream.Close();
  70.                     }
  71.                     catch (Exception)
  72.                     {
  73.                     }
  74.  
  75.                     try
  76.                     {
  77.                         stream.Close();
  78.                     }
  79.                     catch (Exception)
  80.                     {
  81.                     }
  82.  
  83.                     try
  84.                     {
  85.                         webResponse.Close();
  86.                     }
  87.                     catch (Exception)
  88.                     {
  89.                     }
  90.  
  91.                     throw new Exception("Exception occured while sending request.", e);
  92.                 }
  93.  
  94.                 throw new Exception("No response received from host.");
  95.             }
  96.  
Feb 19 '08 #1
0 1525

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Larry Woods | last post by:
I have a server page that has served an HTML page with a "Cancel" button on it, BUT this server page has not completed and is running a 15-30 second process before it completes. I want the browser...
1
by: Ryan McLean | last post by:
Hi everyone! What is happening is the method: sub_btnSubmitClicked is being executed every time any other object with a Handler is executed. I am trying not to use the withevents and handles...
4
by: louise raisbeck | last post by:
I have this scenario (simplified) function addnewdata () { check for partial match already in db for information entered by user if (partialmatch succeeds) { open new window aspx page (using...
6
by: Ken Varn | last post by:
I have an ASP.NET form that may take a very long time to process a particular request. If the user closes the browser window, the request will continue to process until it completes. This is a...
16
by: wizard04 | last post by:
On my WinXP machine, with both IE6 and Firefox 1.0, response.redirect and server.transfer take about a minute. But on my WinNT machine with IE5.5, it works instantly. What's going on?
0
by: Ken T. | last post by:
I have questions regarding what happens on the web server side during an asynchronous web method call when the client aborts its request via .Abort() call. Background: My client app...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
4
by: AshishMishra16 | last post by:
HI friends, I am using the Flex to upload files to server. I m getting all the details about the file, but I m not able to upload it to Server. Here is the code i m using for both flex & for...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.