Windows phone 8 windows phone 8中的Http post呼叫

Windows phone 8 windows phone 8中的Http post呼叫,windows-phone-8,http-post,Windows Phone 8,Http Post,我是windows phone 8开发的新手 您能否帮助我如何在windows phone 8中通过http post呼叫将xml数据发送到服务器? 谢谢这是我在项目中使用的示例代码。根据您的需要进行修改 HttpWebRequest req = (HttpWebRequest)WebRequest.Create("url to submit to"); req.Method = "POST"; //Add a Header something like this //req.Headers[

我是windows phone 8开发的新手

您能否帮助我如何在windows phone 8中通过http post呼叫将xml数据发送到服务器?


谢谢

这是我在项目中使用的示例代码。根据您的需要进行修改

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("url to submit to");
req.Method = "POST";

//Add a Header something like this
//req.Headers["SOAPAction"] = "http://asp.net/ApplicationServices/v200/AuthenticationService/Login";

req.ContentType = "text/xml; charset=utf-8";

//req.UserAgent = "PHP-SOAP/5.2.6";

string xmlData = @"<?xml version=""1.0"" encoding=""UTF-8""?>Your xml data";

// Convert the string into a byte array.
byte[] byteArray = Encoding.UTF8.GetBytes(xmlData);
req.Headers[HttpRequestHeader.ContentLength] = byteArray.Length.ToString();

req.BeginGetRequestStream(ar =>
{
    using (var requestStream = req.EndGetRequestStream(ar))
    {
        // Write the body of your request here
        requestStream.Write(byteArray, 0, xmlData.Length);
    }

    req.BeginGetResponse(a =>
    {
        try
        {
            var response = req.EndGetResponse(a);
            var responseStream = response.GetResponseStream();

            using (var streamRead = new StreamReader(responseStream))
            {
                // Parse the response message here
                string responseString = streamRead.ReadToEnd();
                //If response is also XML document, parse it like this
                XDocument xdoc = XDocument.Parse(responseString);
                string result = xdoc.Root.Value;

                if (result == "true")
                {
                    //Do something here
                }
                else
                    Dispatcher.BeginInvoke(() =>
                    {
                        //result failed
                        MessageBox.Show("Some error msg");
                    });
            }
        }
        catch (Exception ex)
        {
            Dispatcher.BeginInvoke(() =>
            {
                //if any unexpected exception occurs, show the exception message
                MessageBox.Show(ex.Message);
            });
        }

    }, null);

}, null);
HttpWebRequest req=(HttpWebRequest)WebRequest.Create(“要提交的url”);
请求方法=“POST”;
//添加一个类似这样的标题
//请求标题[“SOAPAction”]=“http://asp.net/ApplicationServices/v200/AuthenticationService/Login";
req.ContentType=“text/xml;charset=utf-8”;
//req.UserAgent=“PHP-SOAP/5.2.6”;
字符串xmlData=@“您的xml数据”;
//将字符串转换为字节数组。
byte[]byteArray=Encoding.UTF8.GetBytes(xmlData);
req.Headers[HttpRequestHeader.ContentLength]=byteArray.Length.ToString();
请求BeginGetRequestStream(ar=>
{
使用(var requestStream=req.EndGetRequestStream(ar))
{
//把你的请求写在这里
Write(byteArray,0,xmlData.Length);
}
请求开始响应(a=>
{
尝试
{
var响应=请求EndGetResponse(a);
var responseStream=response.GetResponseStream();
使用(var streamRead=newstreamreader(responseStream))
{
//在这里解析响应消息
字符串responseString=streamRead.ReadToEnd();
//若响应也是XML文档,则按如下方式解析它
XDocument xdoc=XDocument.Parse(responseString);
字符串结果=xdoc.Root.Value;
如果(结果=“真”)
{
//在这里做点什么
}
其他的
Dispatcher.BeginInvoke(()=>
{
//结果失败
Show(“一些错误消息”);
});
}
}
捕获(例外情况除外)
{
Dispatcher.BeginInvoke(()=>
{
//如果发生任何意外异常,请显示异常消息
MessageBox.Show(例如Message);
});
}
},空);
},空);