none
Advise on efficient way of calling a Web request Asunchronously RRS feed

  • Question

  • Dear all,

    I need to place a call to a public web service quite often through difference instances.

    So far I have build a test program where my call goes as follow:

     try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
                    request.Method = "POST";
                    request.ContentType = "application/json";
                    request.ContentLength = REWARDDATAREQUEST.Length;
                    System.Net.ServicePointManager.Expect100Continue = false;
    
                    StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
                    requestWriter.Write(REWARDDATAREQUEST);
                    requestWriter.Close();
    
                    WebResponse webResponse = request.GetResponse();
                    Stream webStream = webResponse.GetResponseStream();
                    StreamReader responseReader = new StreamReader(webStream);
                    response = responseReader.ReadToEnd();
                    responseReader.Close();
                    Regex r = new Regex(@"result.*?\:(\w+)");
                    Result = r.Match(response).Groups[1].Value;
                }

    The process that takes most of the time here is the  Stream webStream = webResponse.GetResponseStream(); which freeze for a while the application (2seconds).

    What could be the best approach to make the call to the request and get the result in a really asynchronous way ?

    I have tried different approach like BeginGetResponse or even task process but seems I mix up things.

    thnaks for sample

    regards

    serge


    Your knowledge is enhanced by that of others.

    • Moved by Bob Shen Monday, March 25, 2013 8:16 AM
    Sunday, March 24, 2013 10:42 AM

Answers