如何为HttpClient PostAsync第二个参数设置HttpContent? [英] How do I set up HttpContent for my HttpClient PostAsync second parameter?

查看:613
本文介绍了如何为HttpClient PostAsync第二个参数设置HttpContent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static async Task<string> GetData(string url, string data)
{
    UriBuilder fullUri = new UriBuilder(url);

    if (!string.IsNullOrEmpty(data))
        fullUri.Query = data;

    HttpClient client = new HttpClient();

    HttpResponseMessage response = await client.PostAsync(new Uri(url), /*expects HttpContent*/);

    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    response.EnsureSuccessStatusCode();
    string responseBody = await response.Content.ReadAsStringAsync();

    return responseBody;
}

PostAsync采用另一个需要为HttpContent的参数.

The PostAsync takes another parameter that needs to be HttpContent.

如何设置HttpContent?在任何地方都没有适用于Windows Phone 8的文档.

How do I set up an HttpContent? There Is no documentation anywhere that works for Windows Phone 8.

如果我执行GetAsync,则效果很好!但它必须是POST,其内容为key ="bla",something ="yay"

If I do GetAsync, it works great! but it needs to be POST with the content of key="bla", something="yay"

//EDIT

非常感谢您的回答...效果很好,但是在这里仍然不确定:

Thanks so much for the answer... This works well, but still a few unsures here:

    public static async Task<string> GetData(string url, string data)
    {
        data = "test=something";

        HttpClient client = new HttpClient();
        StringContent queryString = new StringContent(data);

        HttpResponseMessage response = await client.PostAsync(new Uri(url), queryString );

        //response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        response.EnsureSuccessStatusCode();
        string responseBody = await response.Content.ReadAsStringAsync();

        return responseBody;
    }

我认为数据"test = something"将在api方面作为发布数据"test"出现,显然不是.另一方面,我可能需要通过发布数据来发布整个对象/数组,因此我认为json将是最好的选择.关于如何获取帖子数据有任何想法吗?

The data "test=something" I assumed would pick up on the api side as post data "test", evidently it does not. On another matter, I may need to post entire objects/arrays through post data, so I assume json will be best to do so. Any thoughts on how I get post data through?

也许是这样的:

class SomeSubData
{
    public string line1 { get; set; }
    public string line2 { get; set; }
}

class PostData
{
    public string test { get; set; }
    public SomeSubData lines { get; set; }
}

PostData data = new PostData { 
    test = "something",
    lines = new SomeSubData {
        line1 = "a line",
        line2 = "a second line"
    }
}
StringContent queryString = new StringContent(data); // But obviously that won't work

推荐答案

找不到解决方法使用HttpContent 以及此博客帖子.

总而言之,您不能直接设置HttpContent的实例,因为它是抽象类.您需要根据需要使用从其派生的类之一. StringContent最有可能,它使您可以在构造函数中设置响应的字符串值,编码和媒体类型.请参阅: http://msdn.microsoft.com/en -us/library/system.net.http.stringcontent.aspx

In summary, you can't directly set up an instance of HttpContent because it is an abstract class. You need to use one the classes derived from it depending on your need. Most likely StringContent, which lets you set the string value of the response, the encoding, and the media type in the constructor. See: http://msdn.microsoft.com/en-us/library/system.net.http.stringcontent.aspx

这篇关于如何为HttpClient PostAsync第二个参数设置HttpContent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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