1.使用当前登录用户
HttpClientHandler hch = new HttpClientHandler();
hch.UseDefaultCredentials = true;
2.使用账户密码,模拟任何账户
HttpClientHandler handler = new HttpClientHandler();
handler.Credentials = new NetworkCredential("username", "password", "demain");
using (HttpClient client = new HttpClient(handler))
{
HttpResponseMessage response = client.GetAsync("http://localhost/webapi/api/demo").Result;
IEnumerable<string> userNames = response.Content.ReadAsAsync<IEnumerable<string>>().Result;
foreach (string userName in userNames)
{
Console.WriteLine(userName);
}
}
Console.Read();