【发布时间】:2017-05-02 22:22:06
【问题描述】:
如何调用 RESTful 服务并传入基本身份验证。这是用户名和密码以及基本授权。
using (var httpClient = new HttpClient())
{
//var request = new StringContent(messageBody);
//request.Headers.ContentType = new MediaTypeHeaderValue("application/json");
httpClient.BaseAddress = new Uri(serviceUrl);
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var credentials = Encoding.ASCII.GetBytes("myadmin:mypassword");
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(credentials));
//var response = await httpClient.PostAsJsonAsync(serviceUrl, customer);
HttpResponseMessage response = await httpClient.PostAsJsonAsync("url here", customer);
}
【问题讨论】:
-
你现在寄什么?什么是服务器响应?
-
目前只是传递对象,但我还需要为用户名和密码设置基本身份验证,我不知道该怎么做
-
@Ksib 可能....但不能重复!
-
对我来说似乎是重复的。解释它的不同之处。您想通过基本身份验证向服务发布帖子。似乎一样。我还用谷歌搜索了你的确切标题,并得到了你想做的结果。 this 是另一个结果
标签: c# asp.net-web-api