WebClient: Credentials + Proxy: (401) Unauthorized - c#

The following is being tested on Windows 8 and 7.
I have this code, which works when not behind a proxy:
var client = new WebClient();
client.Credentials = new NetworkCredential(USER, PASS);
client.DownloadFile("http://mydns/subfolder/myfile.zip")
The client USER and PASS is a local user on the server.
When I combine that with a proxy, I keep getting the 401 error:
var proxy = WebRequest.DefaultWebProxy;
proxy.Credentials = CredentialCache.DefaultCredentials;
client.Proxy = proxy;
The client has the correct proxy configured, because with the browser I can go to the wanted url.
I also tried with manually setting the proxy Credentials, but that also didn't work:
WebProxy proxy;
if(_proxySetting.Port != null)
proxy = new WebProxy(_proxySetting.Server, _proxySetting.Port);
else
proxy = new WebProxy(_proxySetting.Server);
if(!string.IsNullOrEmpty(_proxySetting.Username))
{
proxy.Credentials = new NetworkCredential(_proxySetting.Username, _proxySetting.Password);
}
client.Proxy = proxy;
I've tried what is explained on the following pages, without any luck:
WebClient generates (401) Unauthorized error
C# webclient and proxy server
http://www.vbforums.com/showthread.php?517076-RESOLVED-2005-WebClient-Credentials-allways-getting-401!
Any help is much appreciated since I'm totally out of ideas.

Related

C# How to set up Webproxy with NTLM Authtification

i wanna set up a webclient with a NTLM authtification proxy, but i have some Problems to hand over the DefaultCredentials. This is my current solution, but it doesn’t work.
textBox.Text = "";
IWebProxy proxy = System.Net.WebRequest.DefaultWebProxy;
proxy.Credentials = CredentialCache.DefaultCredentials;
WebClient client = new WebClient
{
Proxy = proxy
};
client.Credentials = CredentialCache.DefaultCredentials;
textBox.Text = client.DownloadString("http://www.google.com");
Here are some referencing Links thatI read, but nothing work for me:
How should I set the default proxy to use default credentials?
C# Connecting Through Proxy
do you have any idea to fix this?

Cannot connect to url in C# with 401 unauthorized error

I'm looking to get the content of a page from
http://12.18.60.199:81
I'm using my corporate network and if I use internet explorer, it prompts for username and password, I type that in and I get the content of the page. I need to do this in C#, but have no luck for the past few hours:
Uri requestUri = null;
Uri.TryCreate("http://12.18.60.199:81", UriKind.Absolute, out requestUri);
NetworkCredential nc = new NetworkCredential(#"username", #"password", "domain");
CredentialCache cache = new CredentialCache();
cache.Add(requestUri, "Basic", nc); //also tried "Anonymous", "Basic", "Digest", "Dpa",
//"External", "Kerberos", "Msn", "Negotiate", "Ntlm", "Sicily"
using (WebClient client = new WebClient())
{
client.Credentials = cache;
using (Stream stream = client.OpenRead("http://12.18.60.199:81"))
using (StreamReader reader = new StreamReader(stream))
{
//stuff
}
}
Keep getting 401 unauthorized, invalid credentials, help!
If I substitute the above address with http://google.com, it'll work, so the code works... username and password have been tested to work in broswer
If you are connecting through a proxy server try adding in your proxy and pass the credentials. For example:
// Prepare web request...
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http://www.test.com");
// proxy details
myRequest.Proxy = new WebProxy("http://10.0.0.1", true);
myRequest.Proxy.Credentials = new NetworkCredential("test", "password", "domain");
I had same issue, and found workaround to add Authorization request header. I've used fiddler to get that base64. Never found proper solution, as you all know temp solutions are long-lasting in out world :)
service.Url = "http://127.0.0.1/";
service.SetRequestHeader("Authorization", "Basic dW8sX3NvYXB6SjRma2pzdXZqNDg5ZA==");
service.PreAuthenticate = true;

.NET: Determining the URL of proxy

I want to pass the proxy to my web service. I found the following code
MyWebService myService = new MyWebService();
System.Net.WebProxy proxyObject =
new System.Net.WebProxy("http://proxyserver:80/", true);
myService.Proxy = proxyObject;
myService.MyMethod();
How do I get "http://proxyserver:80/" dynamically?
Try this:
IWebProxy proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = CredentialCache.DefaultCredentials;
webRequest.Proxy = proxy;
The GetSystemWebProxy method reads the system proxy settings set up in Internet Explorer options. If that's not what you want, I'm afraid there is no way to automagically determine the address of some unknown proxy server.

WebClient returning: response if no proxy given

I want to upload a file with WebClient.UploadFile and is as
string proxyserver = "http://10.0.0.127:8080/";
IWebProxy proxy = new WebProxy(proxyserver);
proxy.Credentials = CredentialCache.DefaultCredentials;
WebClient Client = new WebClient();
Client.Headers.Add("UA-CPU", "x86");
Client.Headers.Add("Accept-Language", "en-us");
Client.Headers.Add("Cache-Control", "no-cache");
Client.Proxy = proxy;
byte[] rawResponse = Client.UploadFile(uri, filename);
string response = System.Text.Encoding.ASCII.GetString(rawResponse);
I am getting remote server returned an error 403 Forbidden and if i comment the proxy i am able to get response, but i can't see response in fiddler, not knowing why? and also how can i work this with proxy

Proxy in .net

I tried accessing web for xml parsing using the code below:
System.Uri proxy = new System.Uri("http://usr:pwd#10.1.121.2:8080");
System.Net.WebRequest.DefaultWebProxy = new System.Net.WebProxy(proxy);
string url = "http://url/";
XmlTextReader reader = new XmlTextReader(url);
richTextBox1.Text = Convert.ToString(reader.Read());
But it's giving an error that proxy authentication needed. I have set the proxy parameters already. So can someone suggest changes needed to make it work?
WebProxy proxy = new WebProxy("172.0.0.1:8080", true);
proxy.Credentials = new NetworkCredential("user", "passw", "domain");
Then, use WebRequest to retrieve the data stream through the proxy.
WebRequest dstream = WebRequest.Create("http://data-stream-url.com/file.ext");
dstream.Proxy = proxy;
You'll have to set Credentials property on the WebRequest.DefaultWebProxy object. CredentialCache.DefaultNetworkCredentials might do the job.

Categories

Resources