我尝试使用发布帖子请求。代码:
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();
});
但我不知道从服务器传输返回值的位置。不要告诉我你怎么得到?
答案 0 :(得分:1)
您可以通过查看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;
}
答案 1 :(得分:0)
var responseContent = await client.PostAsync(url, contentPost)
.ContinueWith(postTask => postTask.Result.EnsureSuccessStatusCode())
.Result.Content.ReadAsStringAsync();