0
votes

I am developing a mobile app from which I am sending an HTTP POST request containing XML for user authentication purpose to a PHP based web service.

The response from the web service is received in the method GetResponseCallback(IAsyncResult asynchronousResult).

See the some portion of the code given below for sending the request.

class XMLHttpRequestHandler
{
    private HttpWebRequest vWebRequestObj;
    private string vXMLString;
    private string vResponse;

    public void SendXMLHttpRequest(string pXMLString)
    {
        vWebRequestObj = (HttpWebRequest)HttpWebRequest.CreateHttp("https://test.mydomain.com/mywebservice.php");
        vWebRequestObj.Method = "PUT";
        vWebRequestObj.ContentType = "text/xml; charset=utf-8";


        // initialize the XML string
        vXMLString = pXMLString;

        // Convert the string into a byte array and calculate the length. 
        vWebRequestObj.ContentLength = Encoding.UTF8.GetBytes(vXMLString).Length;

         // start the asynchronous operation
         vWebRequestObj.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), vWebRequestObj);
    }

    private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
    {
         // create the WebRequestObject
         HttpWebRequest vWebRequestObj = (HttpWebRequest)asynchronousResult.AsyncState;

         // End the operation and get the stream for writing the XML.
         Stream postStream = vWebRequestObj.EndGetRequestStream(asynchronousResult);

         // Convert the string into a byte array. 
         byte[] byteArray = Encoding.UTF8.GetBytes(vXMLString);

         // Write to the request stream.
         postStream.Write(byteArray, 0, byteArray.Length);
         postStream.Close();

         // Start the asynchronous operation to get the response
         vWebRequestObj.BeginGetResponse(new AsyncCallback(GetResponseCallback), vWebRequestObj);
     }

     private void GetResponseCallback(IAsyncResult asynchronousResult)
     {
         // create the WebRequestObject
         HttpWebRequest vWebRequestObj = (HttpWebRequest)asynchronousResult.AsyncState;

         // End the operation
         HttpWebResponse vWebResponseObj = (HttpWebResponse)vWebRequestObj.EndGetResponse(asynchronousResult);

         // get the response stream
         Stream ResponseStream = vWebResponseObj.GetResponseStream();

         // create the stream reader object
         StreamReader ResponseStreamReader = new StreamReader(ResponseStream);

         // read the stream
         vResponse = ResponseStreamReader.ReadToEnd();

         // output the stream
         System.Diagnostics.Debug.WriteLine(vResponse);

        // Close the stream object
        ResponseStream.Close();
        ResponseStreamReader.Close();

        // Release the HttpWebResponse
        vWebResponseObj.Close();
    }
}

I am calling the method SendXMLHttpRequest(string pXMLString) from some other class in my project like this

string XMLString = "<testxml><username>abcd</username><password>xyz</password></testxml>";

// send the XML HTTP request
XMLHttpRequestHandler ob = new XMLHttpRequestHandler();
string webserviceresponse = ob.SendXMLHttpRequest(XMLString);    

The problem I am facing is that I am not able to figure out how can I receive the response string in the variable webserviceresponse in the calling code. How can I solve this ?

1
Hope your xmlstring be var XMLString = @"<testxml><username>abcd</username><password>xyz</password>";now he who must not be named.
I have corrected the variable XMLString...... why you have used @ ??Mayank

1 Answers

1
votes

In your class XMLHttpRequestHandler create an Action:

class XMLHttpRequestHandler

{
public Action<string> CallbackComplete;
}

And in your method GetResponseCallback you invoke that action if it's set.

if(CallbackComplete != null){CallbackComplete(vResponse);}

In your caller class you set up a method that is called when this happens(you get your response):

private void OnObCallbackComplete(string str)
{
//Do stuff
}

ob.CallbackComplete = OnObCallbackComplete;