2013-08-28 104 views
0

我正在開發一個移動應用程序,從中我向其發送包含用於用戶身份驗證的XML的HTTP POST請求到基於PHP的Web服務。從WP8中的異步方法調用返回一個值C#

來自Web服務的響應在方法GetResponseCallback(IAsyncResult asynchronousResult)中收到。

請參閱下面給出的用於發送請求的部分代碼。

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中。我該如何解決這個問題?

+0

希望你的xmlstring是var XMLString = @「 abcd xyz」; –

+0

我已更正變量'XMLString' ......爲什麼你使用了@? – Mayank

回答

1

在你的類XMLHttpRequestHandler創建行動:

class XMLHttpRequestHandler 

{ 
public Action<string> CallbackComplete; 
} 

而在你的方法GetResponseCallback你,如果它的設置調用該動作。

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

在您的主叫類,你建立了一個名叫當這種情況發生的方法(你會得到你的迴應):

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

ob.CallbackComplete = OnObCallbackComplete;