I have some code that's throwing a 407 unauthorized exception.
I am trying to download a file and below is my example code. I've tried with netcredentials and webproxy but in vain.
WebClient webClient = new WebClient();
NetworkCredential netCred=new NetworkCredential();
netCred.UserName="<<userid>>";
netCred.Password="<<password>>";
netCred.Domain="<<windowsdomainname>>";
webClient.Credentials = netCred;
WebProxy wp = new WebProxy();
wp.Credentials = netCred;
wp.Address = new Uri(#"http://proxy-xx.xxxx.co.uk:8080/proxy.pac");
webClient.Proxy = wp;
webClient.DownloadFile("http://www.win-rar.com/postdownload.html?&L=0", #"c:\winrar.exe");
I was getting 407 before finding your question. Modified your source to the following, which works for me:
try
{
var netCred = new NetworkCredential { UserName = "ununun", Password = #"pwpwpw", Domain = #"domain" };
var webProxy = new WebProxy { Credentials = netCred };
var webClient = new WebClient { Proxy = webProxy };
webClient.DownloadFile(url, saveFileName);
}
catch (Exception ex)
{
Console.WriteLine("Exception:\n{0}\n{1}", ex.Message, ex.StackTrace);
return;
}
This is what I do yesterday.
WebClient client = new WebClient();
ICredentials cred;
cred = new NetworkCredential(user, password);
client.Proxy = new WebProxy("xxxx-proxy", true, null, cred);
Hope it help !
Related
I am trying to save my images using weblclient Object.
Here it is my code..
try
{
WebClient client = new WebClient();
NetworkCredential nc = new NetworkCredential("****", "*****");
Uri addy = new Uri(#"\\104.199.191.5\Images\");
client.Credentials = nc;
byte[] arrReturn = client.UploadFile(addy, "Post", #"E:\free-html-admin-templates.JPG");
}
catch (Exception ex)
{
}
Right now I am getting error Network path not found..
Where am I wrong ??
This is the code I currently have
using (WebClient client = new WebClient()) {
WebProxy proxy = new WebProxy();
proxy.Address = new Uri(96.44.147.138:6060);
proxy.Credentials = new NetworkCredential(proxyUsername.Text, proxyPassword.Text);
proxy.UseDefaultCredentials = false;
proxy.BypassProxyOnLocal = false;
Console.WriteLine(client.DownloadString("http://bot.whatismyipaddress.com/"));
}
The proxy needs credentials.
I get an error on line proxy.Address = new Uri(96.44.147.138:6060);
saying
"The URI scheme is not valid."
Not sure what kind of value it's expecting
The Uri should consist of scheme host and optiona port. So you should use
proxy.Address = new Uri("http://96.44.147.138:6060");
Must be like;
using (var client = new WebClient())
{
var proxy = new WebProxy();
proxy.Address = new Uri("http://96.44.147.138:6060");
proxy.Credentials = new NetworkCredential(proxyUsername.Text, proxyPassword.Text);
proxy.UseDefaultCredentials = false;
proxy.BypassProxyOnLocal = false;
Console.WriteLine(client.DownloadString("http://bot.whatismyipaddress.com/"));
}
Example edit: Setting a global HTTP proxy in C# and .NET client classes
I have the following code running in a windows form Application:
WebClient client = new WebClient();
WebProxy wp = new WebProxy("http://50.35.125.91:81/");
client.UseDefaultCredentials = true;
wp.Credentials = CredentialCache.DefaultCredentials;
wp.Credentials = new NetworkCredential("matif", "yyy", "xyz");
client.Proxy = wp;
client.DownloadFile("http://50.35.125.91:81/abc/AppUpdate.xml", #"D:\abc.xml");
Each time, I get the following exception
The remote server returned an error: (401) Unauthorized.
I know for sure the credentials are valid,
Provide credentials to proxy and client:
using (WebClient client = new WebClient())
{
WebProxy wp = new WebProxy("http://50.35.125.91:81/");
wp.Credentials = new NetworkCredential("matif", "yyy", "xyz");
client.UseDefaultCredentials = false;
client.Credentials = wp.Credentials;
client.Proxy = wp;
client.DownloadFile("http://50.35.125.91:81/abc/AppUpdate.xml", #"D:\abc.xml");
}
You are using the default credentials. The UseDefaultCredentials should be set to false instead of true:
client.UseDefaultCredentials = false;
Also this line you don't need at all:
wp.Credentials = CredentialCache.DefaultCredentials;
Try this:
using (var Client = new WebClient())
{
Client.Credentials = new NetworkCredential("matif", "yyy", "xyz");
Client.DownloadFile("http://50.35.125.91:81/abc/AppUpdate.xml", #"D:\abc.xml");
}
Hi how do I get the source of an html page through a proxy. When I use the code below I get an error saying "Proxy Authentication Required." and I have to go through a proxy.
Dim client As New WebClient()
Dim htmlCode As String = client.DownloadString("http://www.stackoverflow.com")
Then use a proxy that does not need authentication
see here for more info
http://msdn.microsoft.com/en-us/library/system.net.webclient.proxy.aspx
string source = GetPageSource("http://www.stackoverflow.com");
private string GetPageSource(string url)
{
string htmlSource = string.Empty;
try
{
System.Net.WebProxy myProxy = new System.Net.WebProxy("Proxy IP", 8080);
using (System.Net.WebClient client = new System.Net.WebClient())
{
client.Proxy = myProxy;
client.Proxy.Credentials = new System.Net.NetworkCredential("username", "password");
htmlSource = client.DownloadString(url);
}
}
catch (WebException ex)
{
// log any exceptions
}
return htmlSource;
}
The following code keeps returning a 401:
String URI = "https://api.opsourcecloud.net/oec/0.9/myaccount";
WebClient webClient = new WebClient();
webClient.Credentials = new NetworkCredential("username", "password");
string s = webClient.DownloadString(URI);
StreamReader reader = new StreamReader( webClient.OpenRead(URI));
On the last line. The password and url are correct. Any idea what I am doing wrong?
Have you tried adding the proxy and a domain?
WebClient client = new WebClient();
client.Proxy = new WebProxy("<<your proxy here>>");
client.Proxy.Credentials = new NetworkCredential("login", "password", "domain");