0

我需要向服务器发送请求,将如下的 xml 数据附加到服务器的 url

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

我会得到如下回复

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

我想解析收到的 xml 数据 在 Windows Phone(7) 中执行此操作的正确方法是什么?首先用 xml 请求 url,然后接收 xml 我是 windows phone 开发的新手,应该使用什么类?

我很困惑 - WebClient WebRequest WebResponse HttpWebRequest HttpWebResponse

编辑:我尝试使用以下代码发送请求,如何接收响应?

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

    }
4

1 回答 1

0

尝试以下步骤

第一步:添加命名空间 using System.Net;

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

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

第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    
            }
        }

现在检查这个

于 2013-08-06T05:59:55.310 回答