首页新闻找找看学习计划

Authorization: Basic 怎么加到header 里

0
[已解决问题] 解决于 2020-01-22 16:05

我在网上查了,修改了code 如下,但是感觉还是没有加进到header里,谁可以帮我看看是什么问题呢?

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; //Hard Code TLS 1.2 for .NET 4.0 //.NET 3.5 or below,TLS 1.2 is not supported
EILWS.GetAccountInfoBinding eil = new EILWS.GetAccountInfoBinding();
NetworkCredential eil_nc = new NetworkCredential(eil_userid, eil_pw);
ICredentials credentials = eil_nc.GetCredential(uri, "Basic");
eil.Credentials = credentials;
eil.PreAuthenticate = true;

            pegData = eil.getAccountInfo(acctNo, acctNoTy, svcNo, svcNoTy);
小柑的主页 小柑 | 初学一级 | 园豆:14
提问于:2019-10-24 12:00
< >
分享
最佳答案
0

HttpWebRequest req = GetWebRequest(url, "POST");
req.ContentType = "application/json;charset=UTF-8";
req.Accept = "application/json";
string encoded = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes("password"));
req.Headers.Add("Authorization", "Basic " + encoded);

应该都差不多 你参考一下

奖励园豆:5
去海边生活 | 初学一级 |园豆:62 | 2019-10-30 10:49

注意"Basic " + encoded。是有一个空格的

去海边生活 | 园豆:62 (初学一级) | 2019-10-30 10:50

@去海边生活:
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;
}

小柑 | 园豆:14 (初学一级) | 2020-01-22 16:03

@去海边生活: 谢谢,最后加上上面code成功了

小柑 | 园豆:14 (初学一级) | 2020-01-22 16:04
其他回答(1)
0
  HttpClient client = new HttpClient();

            client.DefaultRequestHeaders.Accept
                  .Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders
                .Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)");
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", urlCollection.Token);
            string json = urlCollection.json;
            HttpContent content = new StringContent(json);
            content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
happydaily | 园豆:648 (小虾三级) | 2019-10-24 17:15
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册