Call API HTTP header Authorization: Basic

2021-01-27 12:14

阅读:559

标签:bytes   user   ted   ret   settings   ase   account   nbsp   false   

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

标签:bytes   user   ted   ret   settings   ase   account   nbsp   false   

原文地址:https://www.cnblogs.com/ganting/p/11936890.html


评论


亲,登录后才可以留言!