C#WP8异步HttpWebRequest发布带有xml请求消息

时间:2014-11-30 17:37:04

标签: c# .net windows-phone-8 httpwebrequest async-await

我已经在这里读了很长时间了,但在这种情况下,我没有再深入了解。 我是Windows Phone开发的新手,面临以下问题。

我正在调用web服务,因为我必须发布xml请求消息。我已经在常规c#中使用了代码(参见下面的代码)

private static string WebRequestPostData(string url, string postData)
{
    System.Net.WebRequest req = System.Net.WebRequest.Create(url);

    req.ContentType = "text/xml";
    req.Method = "POST";

    byte[] bytes = System.Text.Encoding.ASCII.GetBytes(postData);
    req.ContentLength = bytes.Length;

    using (Stream os = req.GetRequestStream())
    {
        os.Write(bytes, 0, bytes.Length);
    }

    using (System.Net.WebResponse resp = req.GetResponse())
    {
        if (resp == null) return null;
            using (System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream()))
        {
            return sr.ReadToEnd().Trim();
        }
    }
}

但对于Windows Phone(8)开发,它需要是异步的。在搜索完网页并尝试各种样本后,我得到了以下代码:

private async void DoCallWS()
{
    string url = "<my_url>";

        // HTTP web request
    var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
    httpWebRequest.ContentType = "text/xml";
    httpWebRequest.Method = "POST";

    // Write the request Asynchronously 
    using (var stream = await Task.Factory.FromAsync<Stream>(httpWebRequest.BeginGetRequestStream,
                                                                 httpWebRequest.EndGetRequestStream, null))
    {
        string requestXml = "<my_request_xml>";

        // convert request to byte array
        byte[] requestAsBytes = Encoding.UTF8.GetBytes(requestXml);

        // Write the bytes to the stream
        await stream.WriteAsync(requestAsBytes , 0, requestAsBytes .Length);

        stream.Position = 0;
        using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
        {
            //return reader.ReadToEnd();

            string result = reader.ReadToEnd();
        }
    }
}

字符串结果具有我正在尝试发送的请求xml消息的值....

我知道async void方法不是首选,但我稍后会修复它。

我也尝试按照Matthias Shapiro(http://matthiasshapiro.com/2012/12/10/window-8-win-phone-code-sharing-httpwebrequest-getresponseasync/)所述的解决方案,但导致代码崩溃

请指出我正确的方向:)

Thnx Frank

1 个答案:

答案 0 :(得分:3)

您所做的只是写入请求流。你错过了从响应中读取的代码。

您收到请求xml的原因是您重置请求流并从该确切流中读取。

您的方法应如下所示:

private async Task DoCallWSAsync()
{
   string url = "<my_url>";

   // HTTP web request
   var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
   httpWebRequest.ContentType = "text/xml";
   httpWebRequest.Method = "POST";

   // Write the request Asynchronously 
   using (var stream = await Task.Factory.FromAsync<Stream>(httpWebRequest.BeginGetRequestStream,
                                                             httpWebRequest.EndGetRequestStream, null))
   {
       string requestXml = "<my_request_xml>";

       // convert request to byte array
       byte[] requestAsBytes = Encoding.UTF8.GetBytes(requestXml);

       // Write the bytes to the stream
       await stream.WriteAsync(requestAsBytes , 0, requestAsBytes .Length);
    }

    using (WebResponse responseObject = await Task<WebResponse>.Factory.FromAsync(httpWebRequest.BeginGetResponse, httpWebRequest.EndGetResponse, httpWebRequest))
    {
        var responseStream = responseObject.GetResponseStream();
        var sr = new StreamReader(responseStream);
        string received = await sr.ReadToEndAsync();

        return received;
    }
}