Call API HTTP header Authorization: Basic

Posted 小柑

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Call API HTTP header Authorization: Basic相关的知识,希望对你有一定的参考价值。

Client           

eil_userid = BaseCmConfig.GetCustomSettings("EILUserId");
            eil_pw = BaseCmConfig.GetCustomSettings("EILPassword");
            eil_pw = FMSEncrypt.Decrypt(eil_pw);
            eil_nc = new NetworkCredential(eil_userid, eil_pw);
            eil_url = "https://" + System.Configuration.ConfigurationManager.AppSettings["EILWS"].ToString();
            eil_credentials = eil_nc.GetCredential(new Uri(eil_url), "Basic");

 

               EILWS.GetAccountInfoBinding eil = new EILWS.GetAccountInfoBinding();
                eil.Credentials = eil_credentials;
                eil.PreAuthenticate = true;

 

Server

         protected override System.Net.WebRequest GetWebRequest(Uri uri)
        {
            HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(uri);
            (request as System.Net.HttpWebRequest).KeepAlive = false;
            if (PreAuthenticate)
            {
                NetworkCredential networkCredentials = Credentials.GetCredential(uri, "Basic");
                if (networkCredentials != null)
                {
                    byte[] credentialBuffer = new UTF8Encoding().GetBytes(networkCredentials.UserName + ":" + networkCredentials.Password);
                    request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(credentialBuffer);
                }
                else
                {
                    throw new ApplicationException("No network credentials");
                }
            }
             return request;
        }

以上是关于Call API HTTP header Authorization: Basic的主要内容,如果未能解决你的问题,请参考以下文章

使用 Alamofire 设置 HTTP Headers 和 HTTP Body

通过 api-call 在 directus 中修补数组不起作用

API验证

将 Header 中的授权从 Zuul apigateway 发送到 auth-microservice

php auth认证

装饰器api