Proxy in .net - c#

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.

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;

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.

How can I read a secure XML URI in C#?

I am trying to read a secure XML document from a URI in C#. I understand the basics of the XmlReader class. However I cannot figure out how to supply a username and password for the URI in code. I get the feeling it has something to do with an XmlSecureResolver object. But I can't figure out how to set the username and password. Can anyone help me with how to set the credentials?
Thanks,
Corey
I think this should do the trick:
WebRequest request = WebRequest.Create(url);
request.Credentials = new NetworkCredential("usernamne", "password");
using (WebResponse response = request.GetResponse())
{
using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
{
// Blah blah...
}
}
A quick Google of the term "XmlReader Authenticate" yields this as the first result:
http://msdn.microsoft.com/en-us/library/47as68k4%28v=vs.71%29.aspx
When resolving a URL to a file that contains the XML data to read, the file may have a restricted access policy. If authentication is required to access a network resource, use the XmlResolver.Credentials property to specify the necessary credentials. If the XmlResolver.Credentials property is not set, then credentials are set to null.
XmlTextReader rdr = new XmlTextReader("http://localhost/bookstore/books.xml");
rdr.XmlResolver.Credentials = CredentialCache.DefaultCredentials;
XmlDocument doc = new XmlDocument();
doc.Load(rdr)
and using different credentials:
NetworkCredential myCred = new NetworkCredential(UserName, SecurelyStoredPassword, domain);
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri("www.contoso.com"), "Basic", myCred);
myCache.Add(new Uri("app.contoso.com"), "Basic", myCred);
reader.XmlResolver.Credentials = myCache;

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

Categories

Resources