WP8 C#

2013-08-28 4 views
0

에서 비동기 메서드 호출에서 값 반환 PHP 기반 웹 서비스에 사용자 인증 목적으로 XML이 포함 된 HTTP POST 요청을 보내는 모바일 앱을 개발 중입니다.WP8 C#

웹 서비스의 응답은 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(); 
    } 
} 

내가 알아낼 수 아니라고 내가 직면하고 문제는이

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

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

처럼 내 프로젝트에 다른 클래스의 방법 SendXMLHttpRequest(string pXMLString)를 호출하고 어떻게 응답 문자열을받을 수 있습니다 호출 코드의 변수 webserviceresponse에 있습니다. 이 문제를 어떻게 해결할 수 있습니까? 수업 XMLHttpRequestHandler에서

+0

원하는 xmlstring을 바래하십시오. XMLString = @ "" abcd xyz "; –

+0

변수'XMLString'을 수정했습니다. 왜 @를 사용 했습니까? – Mayank

답변

1

가 액션을 만듭니다

class XMLHttpRequestHandler 

{ 
public Action<string> CallbackComplete; 
} 

그리고이 설정되어있는 경우 해당 조치를 호출 GetResponseCallback 당신의 방법에

. 발신자 클래스에서

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

당신은 (당신이 당신의 응답을받을)이 발생했을 때 호출되는 방법을 설정 :

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

ob.CallbackComplete = OnObCallbackComplete;