C# How to set up Webproxy with NTLM Authtification - c#

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?

Related

WebClient: Credentials + Proxy: (401) Unauthorized

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.

Get proxy from IE lan settings in C#

I want to progrmatically(C#) get the proxy that is set in "IE -> Lan Settings" and i used the following code from this stackoverflow question
WebProxy proxy = (WebProxy) WebRequest.DefaultWebProxy;
but the problem is that I get the following exception:-
Cannot cast 'System.Net.WebRequest.DefaultWebProxy' (which has an
actual type of 'System.Net.WebRequest.WebProxyWrapper') to
'System.Net.WebProxy'
So how do I get the WebProxy object so that I can check if the proxy is set or not?
EDIT :
I got it work ; i just had to know if a specific url was bypasssed or not so i did this
Uri rpkgURI = new Uri("%url%", UriKind.RelativeOrAbsolute);
IWebProxy webProxy = WebRequest.GetSystemWebProxy();
bool isBypass;
if (webProxy != null)
{
isBypass = webProxy.IsBypassed(rpkgURI);
}
else
{
isBypass = true;
}
Do you need to cast it to a WebProxy object? Many implementations that accept a proxy now take an IWebProxy, and the DefaultWebProxy implements IWebProxy.
For example:
IWebProxy proxy2 = WebRequest.DefaultWebProxy;
WebClient wc = new WebClient();
wc.Proxy = proxy2;
Or even more succintly:
WebClient wc = new WebClient();
wc.Proxy = WebRequest.DefaultWebProxy;

.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 and expect100continue

What is the best way to set expect100continue when using WebClient(C#.NET). I have this code below, I still see 100 continue in the header. Stupid apache still complains with 505 error.
string url = "http://aaaa.com";
ServicePointManager.Expect100Continue = false;
WebClient service = new WebClient();
service.Credentials = new NetworkCredential("username", "password");
service.Headers.Add("Content-Type","text/xml");
service.UploadStringCompleted += (sender, e) => CompleteCallback(BuildResponse(e));
service.UploadStringAsync(new Uri(url), "POST", query);
Note: If I put the above in a console app and let it run - then I do not see the headers in fiddler. But, my code is embedded in a user library which is loaded by a WPF app. So, Is there more to Expect100Continue in terms of thread, initialization, etc. Now, I think it is more of my code issue.
You need to set the Expect100Continue property on the ServicePoint used for the URI you're accessing:
var uri = new Uri("http://foo.bar.baz");
var servicePoint = ServicePointManager.FindServicePoint(uri);
servicePoint.Expect100Continue = false;
The only way to do this is to create an override.
public class ExpectContinueAware : System.Net.WebClient
{
protected override System.Net.WebRequest GetWebRequest(Uri address)
{
System.Net.WebRequest request = base.GetWebRequest(address);
if (request is System.Net.HttpWebRequest)
{
var hwr = request as System.Net.HttpWebRequest;
hwr.ServicePoint.Expect100Continue = false;
}
return request;
}
}
This works perfect.
Try to create the WebClient instanse after you change Expect100Continue to false. Or use a Webrequest instead of a WebClient
One of my below approach also worked,
In my constructor of class (service calling), you can set System.Net.ServicePointManager.Expect100Continue = false;
and then I have created BasicHttpBinding object and continued, it executed successfully.
You can modify your Web/App.Config, by adding:
<system.net>
<settings>
<servicePointManager expect100Continue="false"/>
</settings>
</system.net>
At least for me, if the faulty server gets patched, I can easily remove the setting without having to recompile.

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