本文介绍了请求 &响应 Windows Phone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

我会得到如下回复

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

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

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

我很困惑——网页客户端网络请求网络响应网页请求HttpWebResponse

I am very confused in -WebClientWebRequestWebResponseHttpWebRequestHttpWebResponse

我尝试了以下代码发送请求,我如何接收响应??

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

    }

推荐答案

尝试以下步骤

第一步:添加命名空间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
            }
        }

现在检查这个

这篇关于请求 &amp;响应 Windows Phone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 20:52
查看更多