Response from System.Net.HttpClient

本文关键字:HttpClient Net System from Response | 更新日期: 2023-09-27 17:49:22

我尝试使用。代码:

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

但是我不知道返回值从服务器传输到哪里。不要告诉我你怎么能得到?

Response from System.Net.HttpClient

您可以查看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();