从WP8 C#中的异步方法调用返回一个值

时间:2013-08-28 06:17:50

标签: c# asynchronous windows-phone-8 return-value

我正在开发一个移动应用程序,我从中发送一个HTTP POST请求,其中包含用于基于PHP的Web服务进行用户身份验证的XML。

通过方法GetResponseCallback(IAsyncResult asynchronousResult)收到来自Web服务的响应。

请参阅下面给出的代码的一部分来发送请求。

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();
    }
}

我从我的项目中的其他类调用方法SendXMLHttpRequest(string pXMLString),如此

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

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

我面临的问题是我无法弄清楚如何在调用代码中的变量webserviceresponse中接收响应字符串。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

在您的类XMLHttpRequestHandler中创建一个Action:

class XMLHttpRequestHandler

{
public Action<string> CallbackComplete;
}

在您的方法GetResponseCallback中,如果设置了该动作,则调用该动作。

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

在您的调用者类中,您设置了一个在发生这种情况时调用的方法(您得到了您的回复):

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

ob.CallbackComplete = OnObCallbackComplete;