在某些网络中需要代理身份验证(407)

时间:2020-02-14 05:34:59

标签: c# httpclient http-status-code-407

我尝试了以下代码来解决代理身份验证问题。但是问题仍然存在。 注意:某些未使用代理的网络,它工作正常。

var INI = new IniFile(@"Settings.ini");
String scredUserName = INI.Read("UserName", "Credentials");     
String sPassword = INI.Read("Password", "Credentials");        
String sAPIKey = INI.Read("APIKey", "Credentials");


string sUserNamePassword = scredUserName + ":" + sPassword;
byte[] byteUserNamePassword = System.Text.ASCIIEncoding.ASCII.GetBytes(sUserNamePassword);
string encodedUserNamePassword = System.Convert.ToBase64String(byteUserNamePassword);

client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", 
encodedUserNamePassword);
client.DefaultRequestHeaders.Add("aw-tenant-code", sAPIKey);

String sProxyUserName = INI.Read("UserName", "Proxy Authentication");
String sProxyPassword = INI.Read("Password", "Proxy Authentication");

string sProxyUserNamePassword = sProxyUserName + ":" + sProxyPassword;
byte[] byteProxyUserNamePassword = System.Text.ASCIIEncoding.ASCII.GetBytes(sProxyUserNamePassword);
string encodedProxyUserNamePassword = System.Convert.ToBase64String(byteProxyUserNamePassword);
client.DefaultRequestHeaders.Add("Proxy-Authorization", "Basic " + encodedProxyUserNamePassword);

1 个答案:

答案 0 :(得分:0)

发布我的问题的答案:- 我应该已经将代理凭据和代理地址一起传递了。

                    WebProxy wbProxy = new WebProxy();
                    Uri newUri = new Uri(sProxyAddress);

                    wbProxy.Address = newUri; 

                    wbProxy.Credentials = new NetworkCredential(sProxyUserName, sProxyPassword);
                    client.Proxy = wbProxy;