簡體   English   中英

如何為我的 HttpClient PostAsync 第二個參數設置 HttpContent?

[英]How do I set up HttpContent for my HttpClient PostAsync second parameter?

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", something="yay" 的內容進行 POST

//編輯

非常感謝您的回答......這很好用,但這里仍然有一些不確定:

    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 最好這樣做。 關於如何獲取發布數據的任何想法?

也許是這樣的:

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

這在Can't find how to use HttpContent以及這篇博文 的一些答案中得到了解答。

總之,你不能直接設置HttpContent的實例,因為它是一個抽象類 您需要根據需要使用從它派生的類之一。 最有可能的StringContent ,它允許您在構造函數中設置響應的字符串值、編碼和媒體類型。 請參閱: http : //msdn.microsoft.com/en-us/library/system.net.http.stringcontent.aspx

要添加到 Preston 的答案,以下是標准庫中可用的HttpContent派生類的完整列表:

信用: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找到它。

當然,您可以與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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM