UI with an asynchronous WebRequest backend RRS feed

  • Question

  • Hi all,

     Here is a basic outline of my current design:

    My UI has a button and a textbox.  I want to click the button and have the results of a webrequest POST transaction be displayed in the textbox.

    Button_Click calls a method in a class called MessageClass.  This method then calls a sendPOST in another class which contains all the webrequest asynchronous implementations.

     Ideally, this sendPost method would return a string representing the response.  However, because of the asynchronous model, the original sendPost method ends, and the response I need is found in the responseProceed method.  Currently, I am using a messy system of delegates to call all the way back to the UI class with the response as a parameter, but I feel like there must be a better way to implement my webrequest class, hopefully with some kind of pseudo-synchronous communication.

     Here are the relevant methods where I deal with the webRequest.  As I said, ideally, the sendPOST method could just return a string...


    1          //i wish this could return the response string!      
    2            public void sendData(String inData, MessageClassSL.completionDelegate inDelegate)
    3            {
    4                request = HttpWebRequest.Create(serviceURL);
    5                request.Method = "POST";
    6                //request.ContentType = "application/x-www-form-urlencoded";
    7                request.ContentType = "text/XML";
    8                tempDelegate = inDelegate;
    10               dataToSend = inData; // global string
    12               request.BeginGetRequestStream(new AsyncCallback(requestProceed), request);
    13           }
    15           private void requestProceed(IAsyncResult asyncResult)
    16           {
    17               HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
    19               StreamWriter postDataWriter = new StreamWriter(request.EndGetRequestStream(asyncResult));
    20               postDataWriter.Write(dataToSend);
    21               postDataWriter.Close();
    23               request.BeginGetResponse(new AsyncCallback(responseProceed), request);
    24           }
    26           private void responseProceed(IAsyncResult asyncResult)
    27           {
    28               HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
    30               HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
    31               StreamReader responseReader = new StreamReader(response.GetResponseStream());
    33               String serverResponse = responseReader.ReadToEnd();
    34               responseReader.Close();
    35               response.Close();
    36               //Console.Write(serverResponse);
    38               tempDelegate.Invoke(serverResponse);
    39           }
    Friday, January 9, 2009 10:01 AM


  • Hi, if you didn't write your sending WebRequest logic in your UI class, an event is necessary. You can have a look at the WebClient class, that is an asynchronous communication function, it has several events (delegates), such OpenReadCompleted and OpenWriteCompleted. The purpose is that the users could know the response is recieved and they can do something with the result.

    So, I think your design is correct, make an event and in UI class handle it. Don't forget Dispatcher.BeginInvoke, because that response must be at another thread.


    Sunday, January 11, 2009 8:59 PM