HttpClient httpClient = new HttpClient();
try
{
string login_url = config.server_host+"/user/login";
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,new Uri(login_url));
var postData = new HttpFormUrlEncodedContent(new List<KeyValuePair<string, string>> {
new KeyValuePair<string,string>("username",name),
new KeyValuePair<string,string>("password",pwd),
});
request.Content = postData;
HttpResponseMessage response = await httpClient.SendRequestAsync(request);
string responseString = await response.Content.ReadAsStringAsync();
this.output(responseString);
}
catch(Exception ex)
{
this.output(ex.Message.ToString());
}
output函数
void output(string content)
{
await new MessageDialog(content).ShowAsync();
}
使用HttpClient实现登录
本文介绍了一种使用C#中的HttpClient发起POST请求以实现用户登录的方法。通过构造包含用户名和密码的表单数据,并将其发送到指定的服务器端点,可以完成用户的认证过程。此外,还展示了如何处理响应数据及异常情况。
882

被折叠的 条评论
为什么被折叠?



