码迷,mamicode.com
首页 > Windows程序 > 详细

Call API HTTP header Authorization: Basic

时间:2019-11-26 17:53:14      阅读:107      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!