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,它工作得很好!但它需要POST,内容为key="bla", something="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端作为post数据“test”,显然它不是。在另一个问题上,我可能需要通过post数据发布整个对象/数组,所以我认为json将是最好的。关于我如何获得post数据有什么想法吗?

也许是这样的:

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

当前回答

为了补充Preston的答案,这里是标准库中可用的HttpContent派生类的完整列表:

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

还有一个假定的ObjectContent,但我无法在ASP中找到它。净的核心。

当然,你可以通过Microsoft.AspNet.WebApi.Client扩展跳过整个HttpContent(你必须导入它才能在ASP中工作)。NET Core现在:https://github.com/aspnet/Home/issues/1558),然后你可以做这样的事情:

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

其他回答

    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();
    }

为了补充Preston的答案,这里是标准库中可用的HttpContent派生类的完整列表:

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

还有一个假定的ObjectContent,但我无法在ASP中找到它。净的核心。

当然,你可以通过Microsoft.AspNet.WebApi.Client扩展跳过整个HttpContent(你必须导入它才能在ASP中工作)。NET Core现在:https://github.com/aspnet/Home/issues/1558),然后你可以做这样的事情:

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

这个问题在“无法找到如何使用HttpContent”的一些答案中得到了回答,也在这篇博客文章中得到了回答。

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