How to use .Net WebProxy class - c#

I need use the .Net WebProxy class to connect a C# application to internet. My proxy is http://proxy.uh.cu/estudiantes.pac and I'm writing the next lines:
var webproxy = new WebProxy("proxy.uh.cu/estudiantes.pac", 80);
webProxy.Credentials = new NetworkCredential("myUserCredential", "myPassword");
Am I making any mistake here?

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?

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.

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