Learn how to a build a cloud-first strategyRegister Now


httpwebrequest trigger an httphandler, no response wanted

Posted on 2009-04-20
Medium Priority
Last Modified: 2012-06-27

we currently have two webservers, i'm trying to make cache system which makes us able to delete a key from the cache in both servers and on different app pools. What i have done is to create a httphandler which i call with querystringparameter called cacheid.

I therefore need to send a request to the httphandler. I see that i need to wait for the response for the http handler. is there a way to skip this. I just want the request to be sent. then right away, free alle resources and contine the executing of the code.my code now lookes like this.
public void DeleteGlobalCacheID(string cacheID)
            foreach (WebServer ws in WebServers)
                foreach (WebApplication wa in WebApplications)
                    DeleteRemoteWebApplicationCache(ws, wa, cacheID);
        private void DeleteRemoteWebApplicationCache(WebServer webServer, WebApplication webApplication, string cacheid)
            string url = webServer.Url.ToString().Remove(webServer.Url.ToString().Length-1) + webApplication.Path.ToString() + "GlobalCacheHandler.axd";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            string data = "cacheid=" + cacheid ;
            req.ContentLength = data.Length;
            SendRequest(req, data);
        private void SendRequest(HttpWebRequest req, string data)
            StreamWriter stOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);

Open in new window

Question by:oslonet
1 Comment
LVL 21

Accepted Solution

MogalManic earned 2000 total points
ID: 24192677
you have to get a response, but you can choose to ignore it.  To do this you can post the requests asyncroniously like this:

public class RequestState
  // This class stores the State of the request.
  const int BUFFER_SIZE = 1024;
  public StringBuilder requestData;
  public byte[] BufferRead;
  public HttpWebRequest request;
  public HttpWebResponse response;
  public Stream streamResponse;
  public RequestState(string Data)
    BufferRead = new byte[BUFFER_SIZE];
    requestData = new StringBuilder(Data);
    request = null;
    streamResponse = null;
        private void SendRequest(HttpWebRequest req, string data)
             // Create an instance of the RequestState and assign the previous myHttpWebRequest
             // object to its request field.  
            RequestState myRequestState = new RequestState(data);  
            myRequestState.request = myHttpWebRequest;
            // Start the asynchronous request.
            req.BeginGetResponse(new AsyncCallback(HandleResponse), myRequestState)
   //When request completes this method checks the results and logs any errors
  private static void RespCallback(IAsyncResult asynchronousResult)
      // State of request is asynchronous.
      RequestState myRequestState=(RequestState) asynchronousResult.AsyncState;
      HttpWebRequest  myHttpWebRequest=myRequestState.request;
      myRequestState.response = (HttpWebResponse) myHttpWebRequest.EndGetResponse(asynchronousResult);
      // Read the response into a Stream object.
      Stream responseStream = myRequestState.response.GetResponseStream();
      // Begin the Reading of the contents of the HTML page and print it to the console.
      responseStream.Read(myRequestState.BufferRead, 0, BUFFER_SIZE);  //Read only the 1st N bytes
    catch(WebException e)
      Console.WriteLine("\nRespCallback Exception raised!");

Open in new window


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question