使用C#中的基本身份验证调用web api
问题描述:
我想通过基本身份验证而不是授权来调用web api。 plesae帮助我。
i尝试使用以下代码,但我的回复是空的。
我尝试过:
Hi,
I want to call web api with basic authentication not authorization. plesae help me.
i tried with below code but am getting null response.
What I have tried:
HttpClient client = new HttpClient();
string authInfo = "raj" + ":" + "34sddff";
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authInfo);
client.BaseAddress = new Uri("http://sample");
HttpResponseMessage response = client.GetAsync(url).ContinueWith(task => task.Result).Result;
// Parse the response body. Blocking!
if (response.IsSuccessStatusCode)
{
var httpResponseResult = response.Content.ReadAsStringAsync().ContinueWith(task => task.Result).Result;
var data = JObject.Parse(httpResponseResult);
var responseCode= data.SelectToken("code").Value();
var message = data.SelectToken("message").Value();
}
答
你可以参考链接用于获得理想的结果 [ ^ ]
you can refer link for achecving your desired result[^]
请点击链接,为我工作简单的C#.NET 4.5 HTTPClient请求使用基本身份验证和代理·GitHub [ ^ ]
Please follow the link, working for me Simple C# .NET 4.5 HTTPClient Request Using Basic Auth and Proxy · GitHub[^]