什么是System.Net.WebClient.UploadString的System.Net.Http.HttpClient命名空间的WinRT的方法吗? [英] What is an alternative of System.Net.WebClient.UploadString in System.Net.Http.HttpClient namespace for WinRT?

查看:291
本文介绍了什么是System.Net.WebClient.UploadString的System.Net.Http.HttpClient命名空间的WinRT的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作从WPF应用程序移植到Windows 8应用。

I am working on porting an application from WPF to a Windows 8 App.

我想知道是否有在 System.Net.Http.HttpClient <像 System.Net.WebClient.UploadString 类似的功能/ code>命名空间(因为 System.Net.WebClient 的WinRT 不可用),如果是的例子非常AP preciated!如果不是有什么别的选择吗?

I would like to know if there is a similar functionality like System.Net.WebClient.UploadString in System.Net.Http.HttpClient namespace (since System.Net.WebClient isn't available in WinRT) If yes an example is greatly appreciated! If not is there any alternative?

在侧面说明我是能够转换 WebClient.DownloadString 为它的等效使用 System.Net.Http.HttpClient 用的Morten Nielsen的职位帮助这里的http://www.sharpgis.net/post/2011/10/05/WinRT-vs-Silverlight-Part-7-Making-WebRequests.aspx

On the side note I was able to convert WebClient.DownloadString to its equivalent using System.Net.Http.HttpClient namespace with the help of Morten Nielsen post here http://www.sharpgis.net/post/2011/10/05/WinRT-vs-Silverlight-Part-7-Making-WebRequests.aspx

推荐答案

是的,你可以使用的 PostAsync 方法

Yes, you would use the PostAsync method.

该方法采用任何一个 乌里 或字符串(就像的上的WebClient类 UploadString 方法)以及一个的 HttpContent 实例

The method takes either a Uri or a string (just like the UploadString method on the WebClient class) as well as an HttpContent instance.

HttpContent 实例,就是要不可知的不同类型的内容,让您不仅指定由您提供的内容(的 ByteArrayContent 对于字节数组, StreamContent < /一>等),但在结构,以及(MultipartFormDataContent)。

The HttpContent instance is meant to be agnostic to different types of content, allowing you to specify not only the mechanism by which you provide the content (ByteArrayContent for an array of bytes, StreamContent for a Stream, etc.) but the structure as well (MultipartFormDataContent).

这是说,还有一个 的StringContent 将发送字符串,像这样:

That said, there is also a StringContent class which will send the string, like so:

//内容。 字符串后=你的内容发布

// The content. string post = "your content to post";

// The client.
using (client = new HttpClient());
{
    // Post.
    // Let's assume you're in an async method.
    HttpResponseMessage response = await client.Post(
        "http://yourdomain/post", new StringContent(post));

    // Do something with the response.
}

如果你需要指定一个 编码 有一个构造函数的编码 ,你会使用这样的:

If you need to specify an Encoding, there's a constructor that takes an Encoding, which you would use like this:

// The client.
using (client = new HttpClient());
{
    // Post.
    // Let's assume you're in an async method.
    HttpResponseMessage response = await client.Post(
        "http://yourdomain/post", new StringContent(post),
         Encoding.ASCII);

    // Do something with the response.
}

从那里,它是处理的 的Htt presponseMessage (如果这对你很重要,如果它不是一个单向操作)当发送响应。

From there, it's a matter of processing the HttpResponseMessage (if that's important to you, if it's not a one-way operation) when the response is sent.

这篇关于什么是System.Net.WebClient.UploadString的System.Net.Http.HttpClient命名空间的WinRT的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆