简体   繁体   English

使用HttpClient时出现未经授权的错误

[英]unauthorized error when using HttpClient

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;

namespace ConsoleProgram
{
    public class Class1
    {
        private const string URL = "https://sun.domain.com/v1/service/token";
        static void Main(string[] args)
        {
            var handler = new HttpClientHandler();
            handler.Credentials = new System.Net.NetworkCredential("admin@client", "admin");
            HttpClient client = new HttpClient(handler);
            //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("{0}:{1}", "admin", "admin"))));
            //  client.BaseAddress = new Uri(URL);
            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));
            // List data response.
            HttpResponseMessage response = client.GetAsync(URL).Result;  // Blocking call!
            String res = response.ToString();
            Console.WriteLine(res);
        }
    }
}

I am getting unauthorized error even though I am passing the correct credentials. 即使我传递正确的凭据,也会出现未经授权的错误。 Any ideas? 有任何想法吗?

I tried almost all the answers posted on StackOverflow. 我尝试了几乎所有发布在StackOverflow上的答案。

For Basic Authentication, you need to send the credentials in an authorization header called "Basic" with base64-encoded "username:password" as the value. 对于基本身份验证,您需要在名为“ Basic”的授权标头中发送凭据,并以base64编码的“ username:password”作为值。 This should get the job done: 这应该完成工作:

var headerVal = Convert.ToBase64String(Encoding.UTF8.GetBytes("admin@client:admin"));
var header = new AuthenticationHeaderValue("Basic", headerVal);
client.DefaultRequestHeaders.Authorization = header;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用HttpClient时,IIS 7.5 Web应用程序返回未授权 - IIS 7.5 Web Application returns Unauthorized when using HttpClient 使用 HttpClient 向 Microsoft Graph 请求用户列表时出现未授权问题 - UnAuthorized issue when request user list to Microsoft Graph by using HttpClient httpclient api 在 c# 中出现未经授权的 401 错误 - httpclient api unauthorized 401 error in c# 带有 csrf_token 响应未授权错误的 HttpClient - HttpClient with csrf_token response unauthorized error 使用 HttpClient 的随机 401 未授权错误 - Random 401 Unauthorized errors using HttpClient 通过HttpClient使用Google Cloud AutoML时收到401“未经授权”错误 - Receiving 401 “Unauthorized” error while using Google Cloud AutoML via HttpClient StatusCode:401,ReasonPhrase:使用C#通过HTTPClient调用Post方法时,显示“未授权” - StatusCode: 401, ReasonPhrase: 'Unauthorized' gets displayed when calling a Post Method through HTTPClient using C# GET 请求在浏览器和 Postman 中有效,但是在使用 HttpClient 和 RestSharp 时我得到了未经授权 - GET request works in browser and Postman, but I get Unauthorized when using HttpClient and RestSharp 使用 HttpClient (.NET) 时出现 SSL 策略错误 - SSL policy error when using HttpClient (.NET) 使用HttpClient连接到API时出现错误 - Getting an error when connecting to an API using HttpClient
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM