如何为我的HttpClient PostAsync第二个参数设置HttpContent?

时间:2013-09-24 01:21:46

标签: c# httpclient 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的参数。

如何设置HttpContent?任何适用于Windows Phone 8的文档都没有。

如果我GetAsync,那就太棒了!但它需要使用key =“bla”的内容进行POST,某事=“yay”

//修改

非常感谢答案......这很有效,但这里还有一些不确定的地方:

    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”,显然它没有。另一方面,我可能需要通过post数据发布整个对象/数组,所以我认为json最好这样做。关于我如何通过数据发布的任何想法?

也许是这样的:

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

3 个答案:

答案 0 :(得分:129)

Can't find how to use HttpContent以及此blog post的部分答案已对此进行了解答。

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

答案 1 :(得分:62)

要添加到Preston的答案,这里是标准库中可用的HttpContent派生类的完整列表:

Credit: https://pfelix.wordpress.com/2012/01/16/the-new-system-net-http-classes-message-content/

信用https://pfelix.wordpress.com/2012/01/16/the-new-system-net-http-classes-message-content/

还有一个假设ObjectContent,但我无法在ASP.NET Core中找到它。

当然,您可以跳过整个HttpContent所有内容以及Microsoft.AspNet.WebApi.Client扩展名(您必须进行导入才能使其在ASP.NET Core中正常运行:{{ 3}})然后你可以做以下事情:

var response = await client.PostAsJsonAsync("AddNewArticle", new Post
{
    Title = "New Article Title",
    Body = "New Article Body"
});

答案 2 :(得分:7)

    public async Task<ActionResult> Index()
    {
        apiTable table = new apiTable();
        table.Name = "Asma Nadeem";
        table.Roll = "6655";

        string str = "";
        string str2 = "";

        HttpClient client = new HttpClient();

        string json = JsonConvert.SerializeObject(table);

        StringContent httpContent = new StringContent(json, System.Text.Encoding.UTF8, "application/json");

        var response = await client.PostAsync("http://YourSite.com/api/apiTables", httpContent);

        str = "" + response.Content + " : " + response.StatusCode;

        if (response.IsSuccessStatusCode)
        {       
            str2 = "Data Posted";
        }

        return View();
    }
相关问题