Request & Response Windows Phone

2019-01-29 10:41发布

问题:

I need to send request to server appending xml data as below to the url of the server

<User>
<MobileNumber>xxxxxxxxxx</MobileNumber>
<UserAgent>yyyyy</UserAgent>
</User>

I will get back response as follows

<User>
<MobileNumber>xxxxxxxxxx</MobileNumber>
<ModelId>zzzzzz</ModelId>
<AuthKey>aaaaaaaaa</AuthKey>
<UserAgent>yyyyy</UserAgent>
</User>

I want to parse the recieved xml data What is the proper way to do this in Windows Phone(7)? first request the url with xml and then receive xml I am new to windows phone development what classes should be used??

I am very confused in - WebClient WebRequest WebResponse HttpWebRequest HttpWebResponse

Edit: I tried the following code to send request, how do I receive the response??

private void Upload()
    {

        WebClient webClient = new WebClient();
        webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
        var uri = new Uri("xxxxxxxxxx", UriKind.Absolute);
        StringBuilder postData = new StringBuilder();
        postData.AppendFormat("{0}={1}", "MobileNumber", HttpUtility.UrlEncode("yyyyyyyyy"));
        postData.AppendFormat("&{0}={1}", "UserAgent", HttpUtility.UrlEncode("WP7"));

        webClient.Headers[HttpRequestHeader.ContentLength] = postData.Length.ToString();
        webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_UploadStringCompleted);
        webClient.UploadProgressChanged += webClient_UploadProgressChanged;
        webClient.UploadStringAsync(uri, "POST", postData.ToString());

    }

回答1:

Try the following steps

Step 1: add the namespace using System.Net;

Step 2:

public void Upload() 
{
WebRequest webRequest;
                webRequest = WebRequest.Create(Url + Mobile_No + Request); 
                webRequest.Method = "POST";
                webRequest.ContentType = "application/x-www-form-urlencoded";
                webRequest.BeginGetRequestStream(newAsyncCallback(GetRequestStreamCallback), webRequest);
}

Step 3:

public void GetRequestStreamCallback(IAsyncResult asynchronousResult)
        {
                webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
                Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);
                string postData = "Test";
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                postStream.Write(byteArray, 0, byteArray.Length);
                postStream.Close();
                webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
            }

Step 4 :

public void GetResponseCallback(IAsyncResult asynchronousResult)
        {
            try
            {
                HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
                HttpWebResponse response;
                response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
                Stream streamResponse = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(streamResponse);
                var Response = streamReader.ReadToEnd();
                streamResponse.Close();
                streamReader.Close();
                response.Close();
                if (Response == "")
                {
                   //show some error msg to the user                    

                }
                else
                {                   
                  //Your response will be available in "Response"                        
                }
            }
            catch (WebException)
            {
                //error    
            }
        }

Check this now