Proxy authentication required(407) in some network

230 Views Asked by At

i tried below code to resolve the proxy authentication issue. But still the issue is persist. Note: Some networks without using Proxy, it is working fine.

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

There are 1 best solutions below

0
Sankar S On

Posting the answer for my questions:- I should have passed Proxy credentials along with proxy address.

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

                    wbProxy.Address = newUri; 

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