httpwebrequest trigger an httphandler, no response wanted

Posted on 2009-04-20
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 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

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now