【问题标题】:Proxy authentication required(407) in some network在某些网络中需要代理身份验证(407)
【发布时间】:2020-05-29 21:53:33
【问题描述】:

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

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);

【问题讨论】:

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


    【解决方案1】:

    贴出我的问题的答案:- 我应该将代理凭据连同代理地址一起传递。

                        WebProxy wbProxy = new WebProxy();
                        Uri newUri = new Uri(sProxyAddress);
    
                        wbProxy.Address = newUri; 
    
                        wbProxy.Credentials = new NetworkCredential(sProxyUserName, sProxyPassword);
                        client.Proxy = wbProxy;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多