簡體   English   中英

來自System.Net.HttpClient的響應

[英]Response from System.Net.HttpClient

我嘗試使用發帖請求。 碼:

var client = new HttpClient();

// This is the postdata
var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("Username:", "1"));
postData.Add(new KeyValuePair<string, string>("Password:", "1"));

HttpContent content = new FormUrlEncodedContent(postData);

client.PostAsync("http://libdiary.liberty-lab.ru/api/v1/login", content).ContinueWith(
    (postTask) =>
    {
        postTask.Result.EnsureSuccessStatusCode();
    });

但是我不知道從服務器將返回值傳輸到哪里。 不要告訴我如何獲得?

您可以通過查看HttpResponseMessage.Content

public async Task<string> PostRequestAsync()
{
    var client = new HttpClient();

    var postData = new List<KeyValuePair<string, string>>();
    postData.Add(new KeyValuePair<string, string>("Username:", "1"));
    postData.Add(new KeyValuePair<string, string>("Password:", "1"));

    HttpContent content = new FormUrlEncodedContent(postData);

    HttpResponseMessage response = await client.PostAsync(
                                   "http://libdiary.liberty-lab.ru/api/v1/login",
                                   content);

    string result = await response.Content.ReadAsStringAsync();
    return result;
}
var responseContent = await client.PostAsync(url, contentPost)
            .ContinueWith(postTask => postTask.Result.EnsureSuccessStatusCode())
            .Result.Content.ReadAsStringAsync();

暫無
暫無

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

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