Response from System.Net.HttpClient

I try to make a post request using. code:

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

But I do not know where the return value is transmitted from the server. Do not tell me how you can get?

Upvotes: 0

Views: 719

Answers (2)

Jesse Miller
Jesse Miller

Reputation: 31

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

Upvotes: 0

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149518

You do it by looking at 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;
}

Upvotes: 1

Related Questions