我在网上查了,修改了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);
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);
应该都差不多 你参考一下
注意"Basic " + encoded。是有一个空格的
@去海边生活:
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;
}
@去海边生活: 谢谢,最后加上上面code成功了
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");