代码之家  ›  专栏  ›  技术社区  ›  vijay

如何通过api c#[复制]在谷歌云中创建postgres数据库

  •  0
  • vijay  · 技术社区  · 3 年前

    我有一个用于REST API的HttpClient。但是,我在设置Authorization标头时遇到了问题。我需要将标头设置为我在执行OAuth请求时收到的令牌。 我看到了一些代码。NET建议如下,

    httpClient.DefaultRequestHeaders.Authorization = new Credential(OAuth.token);
    

    但是,在WinRT中不存在Credential类。有人知道如何设置Authorization标头吗?

    0 回复  |  直到 5 年前
        1
  •  1181
  •   Ryan Lundy    6 年前

    因此,方法如下,

    httpClient.DefaultRequestHeaders.Authorization =
        new AuthenticationHeaderValue("Bearer", "Your Oauth token");
    
        2
  •  452
  •   Otabek Kholikov    5 年前
    request.DefaultRequestHeaders.Authorization = 
        new AuthenticationHeaderValue(
            "Basic", Convert.ToBase64String(
                System.Text.ASCIIEncoding.ASCII.GetBytes(
                   $"{yourusername}:{yourpwd}")));
    
        3
  •  113
  •   Willie Cheng danbulochkin    6 年前

    我正在寻找一个好的方法来处理这个问题,我也在研究同样的问题。希望这个答案能帮助像我这样有同样问题的人。

    using (var client = new HttpClient())
    {
        var url = "https://www.theidentityhub.com/{tenant}/api/identity/v1";
        client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
        var response = await client.GetStringAsync(url);
        // Parse JSON response.
        ....
    }
    

    参考来自 https://www.theidentityhub.com/hub/Documentation/CallTheIdentityHubApi

        4
  •  73
  •   Philippe    5 年前

    因为它是一个 重用HttpClient实例的良好实践 ,为 性能和端口耗尽问题 ,而且因为没有一个答案能给出这个解决方案(甚至会引导你走向不良做法:()),我在这里放了一个链接,指向我对类似问题的答案:

    https://stackoverflow.com/a/40707446/717372

    关于如何正确使用HttpClient的一些来源:

        5
  •  53
  •   Amankhani MohammadJavad    4 年前

    我建议你:

    HttpClient.DefaultRequestHeaders.Add("Authorization", "Bearer <token>");
    

    然后你可以这样使用它:

    var response = await client.GetAsync(url);
    if (response.IsSuccessStatusCode)
    {
        responseMessage = await response.Content.ReadAsAsync<ResponseMessage>();
    }
    
        6
  •  39
  •   Jourmand    4 年前

    如果你想发送 HttpClient 使用Bearer Token请求,此代码可能是一个很好的解决方案:

    var requestMessage = new HttpRequestMessage
    {
        Method = HttpMethod.Post,
        Content = new StringContent(".....", Encoding.UTF8, "application/json"),
        RequestUri = new Uri(".....")
    };
    
    requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "Your token");
    
    var response = await _httpClient.SendAsync(requestMessage);
    
        7
  •  26
  •   Guillaume Racicot    5 年前

    我正在设置持票人令牌

    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
    

    它在一个端点工作,但在另一个端点不工作。问题是我的病例较低 b "bearer" 。更改后,它现在适用于我正在使用的两个api。如果你甚至不把它看作是寻找针的大海捞针,那么很容易错过。

    确保有 "Bearer" -资本。

        8
  •  22
  •   James Skemp    3 年前

    使用基本授权和Json-参数。

    using (HttpClient client = new HttpClient())
    {
        var request_json = "your json string";
    
        var content = new StringContent(request_json, Encoding.UTF8, "application/json");
    
        var authenticationBytes = Encoding.ASCII.GetBytes("YourUsername:YourPassword");
    
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                Convert.ToBase64String(authenticationBytes));
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
        var result = await client.PostAsync("YourURL", content);
    
        var result_string = await result.Content.ReadAsStringAsync();
    }
    
        9
  •  15
  •   LENG UNG    6 年前

    使用C#HttpClient设置基本身份验证。以下代码对我有效。

       using (var client = new HttpClient())
            {
                var webUrl ="http://localhost/saleapi/api/";
                var uri = "api/sales";
                client.BaseAddress = new Uri(webUrl);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.ConnectionClose = true;
    
                //Set Basic Auth
                var user = "username";
                var password = "password";
                var base64String =Convert.ToBase64String( Encoding.ASCII.GetBytes($"{user}:{password}"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",base64String);
    
                var result = await client.PostAsJsonAsync(uri, model);
                return result;
            }
    
        10
  •  12
  •   emp    4 年前

    如果你想重复使用 HttpClient ,建议不要使用 DefaultRequestHeaders 因为它们用于随每个请求一起发送。

    你可以试试这个:

    var requestMessage = new HttpRequestMessage
        {
            Method = HttpMethod.Post,
            Content = new StringContent("...", Encoding.UTF8, "application/json"),
            RequestUri = new Uri("...")
        };
    
    requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Basic", 
        Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes($"{user}:{password}")));
    
    var response = await _httpClient.SendAsync(requestMessage);
    
        11
  •  11
  •   Jonas Stensved    3 年前

    对于现在(2021年)找到这条旧线索的人,请查看 this documentation 关于 HttpClientFactory 它是可注射的,也会在每次请求时重新运行,避免过期的令牌,这将使其对承载令牌、生成的客户端、池等有用。

    TL;DR:使用 HttpClient工厂 以及a DelegatingHandler 它将作为中间件处理您配置的客户端的所有传出请求。

    这就是我为Azure身份(由Azure管理)添加承载者的方式,但你当然可以随心所欲地获得令牌;

    using Microsoft.Azure.Services.AppAuthentication;
    using System.Net.Http;
    using System.Threading;
    using System.Threading.Tasks;
    
    public class BearerTokenHandler : DelegatingHandler
        {
            public BearerTokenHandler(AzureServiceTokenProvider tokenProvider, string resource)
            {
                TokenProvider = tokenProvider;
                Resource = resource;
            }
    
            public AzureServiceTokenProvider TokenProvider { get; }
            public string Resource { get; }
    
            protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
            {
                if (!request.Headers.Contains("Authorization"))
                {
                    // Fetch your token here
                    string token = await TokenProvider.GetAccessTokenAsync(Resource);
                    request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
                }
    
                return await base.SendAsync(request, cancellationToken);
            }
        }
    

    我在Startup中这样配置我的类型化客户端(用NSwag生成);

       var accessTokenProvider = new AzureServiceTokenProvider("<your-connection-string-for-access-token-provider>");
    
      builder.Services.AddHttpClient<IOrdersClient, OrdersClient>().ConfigureHttpClient(async conf =>
                {
                    conf.BaseAddress = new Uri("<your-api-base-url>");
                }).AddHttpMessageHandler(() => new BearerTokenHandler(accessTokenProvider, "https://your-azure-tenant.onmicrosoft.com/api"));
    

    然后,您可以在任何地方注入IOrdersClient,所有请求都将有承载者。

        12
  •  10
  •   Dayan    7 年前

    我是这样做的:

    using (HttpClient httpClient = new HttpClient())
    {
       Dictionary<string, string> tokenDetails = null;
       var messageDetails = new Message { Id = 4, Message1 = des };
       HttpClient client = new HttpClient();
       client.BaseAddress = new Uri("http://localhost:3774/");
       var login = new Dictionary<string, string>
           {
               {"grant_type", "password"},
               {"username", "sa@role.com"},
               {"password", "lopzwsx@23"},
           };
       var response = client.PostAsync("Token", new FormUrlEncodedContent(login)).Result;
       if (response.IsSuccessStatusCode)
       {
          tokenDetails = JsonConvert.DeserializeObject<Dictionary<string, string>>(response.Content.ReadAsStringAsync().Result);
          if (tokenDetails != null && tokenDetails.Any())
          {
             var tokenNo = tokenDetails.FirstOrDefault().Value;
             client.DefaultRequestHeaders.Add("Authorization", "Bearer " + tokenNo);
             client.PostAsJsonAsync("api/menu", messageDetails)
                 .ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode());
          }
       }
    }
    

    这段YouTube视频对我帮助很大。请检查一下。 https://www.youtube.com/watch?v=qCwnU06NV5Q

        13
  •  8
  •   MPJ567    5 年前

    6年后,但添加此内容以防对某人有所帮助。

    https://www.codeproject.com/Tips/996401/Authenticate-WebAPIs-with-Basic-and-Windows-Authen

    var authenticationBytes = Encoding.ASCII.GetBytes("<username>:<password>");
    using (HttpClient confClient = new HttpClient())
    {
      confClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", 
             Convert.ToBase64String(authenticationBytes));
      confClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(Constants.MediaType));  
      HttpResponseMessage message = confClient.GetAsync("<service URI>").Result;
      if (message.IsSuccessStatusCode)
      {
        var inter = message.Content.ReadAsStringAsync();
        List<string> result = JsonConvert.DeserializeObject<List<string>>(inter.Result);
      }
    }
    
        14
  •  5
  •   romelmederos    5 年前

    UTF8选项

    request.DefaultRequestHeaders.Authorization = 
    new AuthenticationHeaderValue(
        "Basic", Convert.ToBase64String(
            System.Text.Encoding.UTF8.GetBytes(
               $"{yourusername}:{yourpwd}")));
    
        15
  •  3
  •   iiminov    7 年前

    使用 AuthenticationHeaderValue System.Net.Http 装配

    public AuthenticationHeaderValue(
        string scheme,
        string parameter
    )
    

    我们可以设置或更新现有 Authorization 我们的标题 httpclient 像这样:

    httpclient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", TokenResponse.AccessToken);
    
        16
  •  3
  •   Joel Wiklund    4 年前

    BaseWebApi.cs

    public abstract class BaseWebApi
    {
        //Inject HttpClient from Ninject
        private readonly HttpClient _httpClient;
        public BaseWebApi(HttpClient httpclient)
        {
            _httpClient = httpClient;
        }
    
        public async Task<TOut> PostAsync<TOut>(string method, object param, Dictionary<string, string> headers, HttpMethod httpMethod)
        {
            //Set url
    
            HttpResponseMessage response;
            using (var request = new HttpRequestMessage(httpMethod, url))
            {
                AddBody(param, request);
                AddHeaders(request, headers);
                response = await _httpClient.SendAsync(request, cancellationToken);
            }
    
            if(response.IsSuccessStatusCode)
            {
                 return await response.Content.ReadAsAsync<TOut>();
            }
            //Exception handling
        }
    
        private void AddHeaders(HttpRequestMessage request, Dictionary<string, string> headers)
        {
            request.Headers.Accept.Clear();
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
            if (headers == null) return;
    
            foreach (var header in headers)
            {
                request.Headers.Add(header.Key, header.Value);
            }
        }
    
        private static void AddBody(object param, HttpRequestMessage request)
        {
            if (param != null)
            {
                var content = JsonConvert.SerializeObject(param);
                request.Content = new StringContent(content);
                request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            }
        }
    

    SubWebApi.cs

    public sealed class SubWebApi : BaseWebApi
    {
        public SubWebApi(HttpClient httpClient) : base(httpClient) {}
    
        public async Task<StuffResponse> GetStuffAsync(int cvr)
        {
            var method = "get/stuff";
            var request = new StuffRequest 
            {
                query = "GiveMeStuff"
            }
            return await PostAsync<StuffResponse>(method, request, GetHeaders(), HttpMethod.Post);
        }
        private Dictionary<string, string> GetHeaders()
        {
            var headers = new Dictionary<string, string>();
            var basicAuth = GetBasicAuth();
            headers.Add("Authorization", basicAuth);
            return headers;
        }
    
        private string GetBasicAuth()
        {
            var byteArray = Encoding.ASCII.GetBytes($"{SystemSettings.Username}:{SystemSettings.Password}");
            var authString = Convert.ToBase64String(byteArray);
            return $"Basic {authString}";
        }
    }
    
        17
  •  1
  •   Jesus Cañedo    10 年前

    如果您从服务接收json或xml,这可能有效,我认为这也可以让您了解标头和T类型是如何工作的,如果您在下一步使用函数MakeXmlRequest(将结果放在xmldocumnet中)和MakeJsonRequest(将json放在您希望具有与json响应相同结构的类中)

    /*-------------------------example of use-------------*/
    MakeXmlRequest<XmlDocument>("your_uri",result=>your_xmlDocument_variable =     result,error=>your_exception_Var = error);
    
    MakeJsonRequest<classwhateveryouwant>("your_uri",result=>your_classwhateveryouwant_variable=result,error=>your_exception_Var=error)
    /*-------------------------------------------------------------------------------*/
    
    
    public class RestService
    {
        public void MakeXmlRequest<T>(string uri, Action<XmlDocument> successAction, Action<Exception> errorAction)
        {
            XmlDocument XMLResponse = new XmlDocument();
            string wufooAPIKey = ""; /*or username as well*/
            string password = "";
            StringBuilder url = new StringBuilder();
            url.Append(uri);
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url.ToString());
            string authInfo = wufooAPIKey + ":" + password;
            authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
            request.Timeout = 30000;
            request.KeepAlive = false;
            request.Headers["Authorization"] = "Basic " + authInfo;
            string documento = "";
            MakeRequest(request,response=> documento = response,
                                (error) =>
                                {
                                 if (errorAction != null)
                                 {
                                    errorAction(error);
                                 }
                                }
                       );
            XMLResponse.LoadXml(documento);
            successAction(XMLResponse);
        }
    
    
    
        public void MakeJsonRequest<T>(string uri, Action<T> successAction, Action<Exception> errorAction)
        {
            string wufooAPIKey = "";
            string password = "";
            StringBuilder url = new StringBuilder();
            url.Append(uri);
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url.ToString());
            string authInfo = wufooAPIKey + ":" + password;
            authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
            request.Timeout = 30000;
            request.KeepAlive = false;
            request.Headers["Authorization"] = "Basic " + authInfo;
           // request.Accept = "application/json";
          //  request.Method = "GET";
            MakeRequest(
               request,
               (response) =>
               {
                   if (successAction != null)
                   {
                       T toReturn;
                       try
                       {
                           toReturn = Deserialize<T>(response);
                       }
                       catch (Exception ex)
                       {
                           errorAction(ex);
                           return;
                       }
                       successAction(toReturn);
                   }
               },
               (error) =>
               {
                   if (errorAction != null)
                   {
                       errorAction(error);
                   }
               }
            );
        }
        private void MakeRequest(HttpWebRequest request, Action<string> successAction, Action<Exception> errorAction)
        {
            try{
                using (var webResponse = (HttpWebResponse)request.GetResponse())
                {
                    using (var reader = new StreamReader(webResponse.GetResponseStream()))
                    {
                        var objText = reader.ReadToEnd();
                        successAction(objText);
                    }
                }
            }catch(HttpException ex){
                errorAction(ex);
            }
        }
        private T Deserialize<T>(string responseBody)
        {
            try
            {
                var toReturns = JsonConvert.DeserializeObject<T>(responseBody);
                 return toReturns;
            }
            catch (Exception ex)
            {
                string errores;
                errores = ex.Message;
            }
            var toReturn = JsonConvert.DeserializeObject<T>(responseBody);
            return toReturn;
        }
    }
    }
    
        18
  •  1
  •   Lee Smith    4 年前

    使用现有的库可能更容易。

    例如,以下扩展方法是随Identity Server 4一起添加的 https://www.nuget.org/packages/IdentityModel/

     public static void SetBasicAuthentication(this HttpClient client, string userName, string password);
        //
        // Summary:
        //     Sets a basic authentication header.
        //
        // Parameters:
        //   request:
        //     The HTTP request message.
        //
        //   userName:
        //     Name of the user.
        //
        //   password:
        //     The password.
        public static void SetBasicAuthentication(this HttpRequestMessage request, string userName, string password);
        //
        // Summary:
        //     Sets a basic authentication header for RFC6749 client authentication.
        //
        // Parameters:
        //   client:
        //     The client.
        //
        //   userName:
        //     Name of the user.
        //
        //   password:
        //     The password.
        public static void SetBasicAuthenticationOAuth(this HttpClient client, string userName, string password);
        //
        // Summary:
        //     Sets a basic authentication header for RFC6749 client authentication.
        //
        // Parameters:
        //   request:
        //     The HTTP request message.
        //
        //   userName:
        //     Name of the user.
        //
        //   password:
        //     The password.
        public static void SetBasicAuthenticationOAuth(this HttpRequestMessage request, string userName, string password);
        //
        // Summary:
        //     Sets an authorization header with a bearer token.
        //
        // Parameters:
        //   client:
        //     The client.
        //
        //   token:
        //     The token.
        public static void SetBearerToken(this HttpClient client, string token);
        //
        // Summary:
        //     Sets an authorization header with a bearer token.
        //
        // Parameters:
        //   request:
        //     The HTTP request message.
        //
        //   token:
        //     The token.
        public static void SetBearerToken(this HttpRequestMessage request, string token);
        //
        // Summary:
        //     Sets an authorization header with a given scheme and value.
        //
        // Parameters:
        //   client:
        //     The client.
        //
        //   scheme:
        //     The scheme.
        //
        //   token:
        //     The token.
        public static void SetToken(this HttpClient client, string scheme, string token);
        //
        // Summary:
        //     Sets an authorization header with a given scheme and value.
        //
        // Parameters:
        //   request:
        //     The HTTP request message.
        //
        //   scheme:
        //     The scheme.
        //
        //   token:
        //     The token.
        public static void SetToken(this HttpRequestMessage request, string scheme, string token);
    
        19
  •  1
  •   Philip Johnson    2 年前

    我偶然发现了这条古老的线索。我遇到的问题是,我知道使用静态HttpClient,但我的令牌需要每59分钟刷新一次。

    所以我本可以使用HttpClientFactory,但因为我的一个项目还在进行中。NET 4.8,我创建了一个继承自HttpClient的类,所以我在所有项目中都有类似的代码。需要一个秘密才能获得令牌(我使用的是identityserver4)。

    然后,我将其设置为DI中的单例(我在这里使用Ninject):

    Bind<MyHttpClient>().ToMethod(c =>
    {
        var accessKey = ConfigurationManager.AppSettings["AccessKey"];
    
        var client = new MyHttpClient(accessKey)
        {
            BaseAddress = new Uri(MyUrls.MyApiBaseUrl)
        };
    
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    
        return client;
    }).InSingletonScope();
    

    然后类本身-以它用来访问的API命名:

    public class MyHttpClient : BaseHttpClient
    {
         private static readonly HttpClient _authHttpClient = new HttpClient();
         private string _secret;
    
         public MyHttpClient(string secret)
         {
             _secret = secret;
         }
    
        /// <summary>
        /// Add the token to each and every request, cached for 1 minute less than the token's lifetime
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
    
            var cacheSeconds = 3600 - 60; // Default of 59 minutes
    
            var token = CacheHelper<string>.Get("MyToken", cacheSeconds * 60, () =>
            {
                var authorityUrl = MyUrls.AuthServerUrl;
    
                // discover endpoints from metadata
                DiscoveryDocumentResponse disco;
                disco = _authHttpClient.GetDiscoveryDocumentAsync(authorityUrl).Result;
                if (disco.IsError)
                {
                    throw new Exception("Error getting discovery document: " + disco.Error);
                }
    
                // request token
                var tokenResponse = _authHttpClient.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
                {
                    Address = disco.TokenEndpoint,
    
                    ClientId = "myapp",
                    ClientSecret = _secret,
                    Scope = "myapi"
                }).Result;
    
                if (tokenResponse.IsError)
                {
                    throw new Exception("Error getting token: " + tokenResponse.Error);
                }
    
                if (tokenResponse.ExpiresIn < cacheSeconds + 60)
                {
                    throw new Exception($"Token expires in {tokenResponse.ExpiresIn}s, which is less than {cacheSeconds + 60}");
                }
    
                if (tokenResponse.ExpiresIn > cacheSeconds + 60)
                {
                    Log.Warn().Message($"Token expiry in {tokenResponse.ExpiresIn}s, which is greater than {cacheSeconds}").Write();
                }
    
                return tokenResponse.AccessToken;
            });
    
            // THIS IS THE BIT - Assign this inside a SendAsync override and you are done!
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
            return base.SendAsync(request, cancellationToken);
        }
    
    }
    

    最后,为了完整起见,我的CacheHelper类看起来像这样:

    public static class CacheHelper<T>
    {
        private static readonly object _locker = new object();
    
        public static T Get(string cacheName, int cacheTimeoutSeconds, Func<T> func)
        {
            var obj = MemoryCache.Default.Get(cacheName, null);
            if (obj != null) return (T)obj;
    
            lock (_locker)
            {
                obj = MemoryCache.Default.Get(cacheName, null);
                if (obj == null)
                {
                    obj = func();
                    var cip = new CacheItemPolicy
                    {
                        AbsoluteExpiration = new DateTimeOffset(DateTime.UtcNow.AddSeconds(cacheTimeoutSeconds))
                    };
                    MemoryCache.Default.Set(cacheName, obj, cip);
                }
            }
    
            return (T)obj;
        }
    }
    
        20
  •  1
  •   Thom Kiesewetter    2 年前

    在net.core中,您可以与Identity Server 4一起使用

    var client = new HttpClient();
    client.SetBasicAuthentication(userName, password);
    

    var client = new HttpClient();
    client.SetBearerToken(token);
    

    看见 https://github.com/IdentityModel/IdentityModel/blob/main/src/Client/Extensions/AuthorizationHeaderExtensions.cs

        21
  •  0
  •   Khurram Jamil    4 年前

    Oauth流程很复杂,总是有一个或多个错误的空间。 我的建议是始终使用样板代码和一组用于OAuth身份验证流程的库。这会让你的生活更轻松。

    这是一组库的链接。 OAuth Libraries for .Net

        22
  •  0
  •   Christian Findlay    3 年前

    首先,我不会使用 HttpClient 直接。犯错太容易了,尤其是在头球方面。这个 DefaultHeadersCollection 它不是不可变的,也不是线程安全的,因为应用程序的其他部分可以更改你的头文件。最好在拨打电话时设置标题。如果你正在使用抽象,建议这样做,因为这个领域的类有点乱,你会想要一个headers集合,并将其放在你的 HttpRequestMessage 在发送之前。您需要确保将内容头放在内容上,而不是消息上。

    Code Reference

    foreach (var headerName in request.Headers.Names)
    {
        //"Content-Type"
        if (string.Compare(headerName, HeadersExtensions.ContentTypeHeaderName, StringComparison.OrdinalIgnoreCase) == 0)
        {
            //Note: not sure why this is necessary...
            //The HttpClient class seems to differentiate between content headers and request message headers, but this distinction doesn't exist in the real world...
            //TODO: Other Content headers
            httpContent?.Headers.Add(HeadersExtensions.ContentTypeHeaderName, request.Headers[headerName]);
        }
        else
        {
            httpRequestMessage.Headers.Add(headerName, request.Headers[headerName]);
        }
    }
    

    这是一个数据结构,您可以使用它来发送包含标头的请求。

    Code Reference

    public interface IRequest
    {
        CancellationToken CancellationToken { get; }
        string? CustomHttpRequestMethod { get; }
        IHeadersCollection Headers { get; }
        HttpRequestMethod HttpRequestMethod { get; }
        AbsoluteUrl Uri { get; }
    }
    
    public interface IRequest<TBody> : IRequest
    {
        TBody? BodyData { get; }
    }
    

    还有一个headers集合:

    Code Reference

    public sealed class HeadersCollection : IHeadersCollection
    {
        #region Fields
        private readonly IDictionary<string, IEnumerable<string>> dictionary;
        #endregion
    
        #region Public Constructors
    
        public HeadersCollection(IDictionary<string, IEnumerable<string>> dictionary) => this.dictionary = dictionary;
    
        public HeadersCollection(string key, string value) : this(ImmutableDictionary.CreateRange(
                    new List<KeyValuePair<string, IEnumerable<string>>>
                    {
                        new(key, ImmutableList.Create(value))
                    }
                    ))
        {
        }
    
        #endregion Public Constructors
    
        #region Public Properties
        public static HeadersCollection Empty { get; } = new HeadersCollection(ImmutableDictionary.Create<string, IEnumerable<string>>());
        public IEnumerable<string> Names => dictionary.Keys;
        IEnumerable<string> IHeadersCollection.this[string name] => dictionary[name];
        #endregion Public Properties
    
        #region Public Methods
        public bool Contains(string name) => dictionary.ContainsKey(name);
    
        public IEnumerator<KeyValuePair<string, IEnumerable<string>>> GetEnumerator() => dictionary.GetEnumerator();
    
        IEnumerator IEnumerable.GetEnumerator() => dictionary.GetEnumerator();
        public override string ToString() => string.Join("\r\n", dictionary.Select(kvp => $"{kvp.Key}: {string.Join(", ", kvp.Value)}\r\n"));
        #endregion
    }
    

    查看所有工作代码和示例 here .

        23
  •  0
  •   Ewan    3 年前

    如果使用Visual Studio IISExpress调试模式并连接到HTTP端口而不是HTTPS端口,则可能会发现身份验证标头被丢弃。

    切换到SLL连接,它们将再次出现。

    不确定为什么,可能是安装程序重定向了http流量,导致身份验证被删除。

        24
  •  0
  •   Fábio Rodrigues Fonseca    2 年前

    您也可以使用以下示例,即它使用IHttpClientFactory:

        readonly IHttpClientFactory _httpClientFactory;
        
        public HTTPClientHelper(IHttpClientFactory httpClientFactory, string clientName = null)
        {
            this._httpClientFactory = httpClientFactory;
        }
    
        public Task<T> GetAsync(string url, string token) {
    
            var client = _httpClientFactory.CreateClient(_clientName);
    
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(JwtBearerDefaults.AuthenticationScheme, token);
    
            using (HttpResponseMessage response = await _client.GetAsync(url)){
              ......
            }
         }
    
        25
  •  -1
  •   Codehelp    10 年前

    这可能有助于设置标题:

    WebClient client = new WebClient();
    
    string authInfo = this.credentials.UserName + ":" + this.credentials.Password;
    authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
    client.Headers["Authorization"] = "Basic " + authInfo;
    
        26
  •  -1
  •   TResponse    5 年前
    static async Task<AccessToken> GetToken()
    {
            string clientId = "XXX";
            string clientSecret = "YYY";
            string credentials = String.Format("{0}:{1}", clientId, clientSecret);
    
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials)));
                List<KeyValuePair<string, string>> requestData = new List<KeyValuePair<string, string>>();
                requestData.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
                FormUrlEncodedContent requestBody = new FormUrlEncodedContent(requestData);
                var request = await client.PostAsync("https://accounts.spotify.com/api/token", requestBody);
                var response = await request.Content.ReadAsStringAsync();
                return JsonConvert.DeserializeObject<AccessToken>(response);
            }
        }