This article is an extension to article Using WebRequest and WebResponse Classes to download files using HTTP, I wrote in year 2001. One user at C# Corner asked me a question, how to download files using HTTP by passing Proxy server. This article explains how to pass proxy server network credentials when downloading files using HTTP.
Passing Network Credentials
If your company is using proxy server to connect to the Internet, you will have to pass proxy settings to download files from the Internet using HTTP. To do so, you need to create a WebProxy instance, and set its Credentials property, which is NetworkCredential and takes userId, password, and network domain.
Here is the code that uses network credentials:
string
result = "";try
{
WebProxy proxy = new WebProxy("http://proxy:80/", true);
proxy.Credentials = new NetworkCredential("userId", "password", "Domain");
WebRequest request = WebRequest.Create(http://www.c-sharpcorner.com);
request.Proxy = proxy;
// Send the 'HttpWebRequest' and wait for response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); System.IO.Stream stream = response.GetResponseStream();
System.Text.Encoding ec = System.Text.Encoding.GetEncoding("utf-8");
System.IO.StreamReader reader = new System.IO.StreamReader(stream, ec);
char [] chars = new Char[256];
int count = reader.Read(chars, 0, 256);
while(count > 0)
{
string str = new String(chars, 0, 256);
result = result + str;
count = reader.Read(chars, 0, 256);
}
response.Close();
stream.Close();
reader.Close();
}
catch(Exception exp)
{
string str = exp.Message;
} Using HTTPS with WebProxy Class
When you create a WebProxy class instance for a proxy server to pass proxy to download files from the Internet using HTTP, code looks like this:
WebProxy proxy = new WebProxy("http://proxy:80/", true);
However, this code will not work when you want to download a file from HTTPS. You need to use the following code to create a WebProxy instance for HTTPS:
WebProxy myProxy = new WebProxy("server.https.com", 443);

Gelasio MarquesPosted Oct 10, 2011, 12:17 PM
I try everything you show but I have always a error at GetResponse() method... =( You can see my problem better at http://stackoverflow.com/questions/7689856/c-cannot-save-file-from-url-with-proxy Can you help me? Thank you in advance
Ahmet Kursad CengizPosted Aug 27, 2011, 6:48 AM
It's a good program for me.
misbah alnawashiPosted Mar 3, 2010, 4:48 AM
thanks alot.
Pradipta GhoshPosted Aug 27, 2009, 2:35 AM
When we are sending webrequest behind the proxy it works fine..But when we want to send email programmatically it fails. But if there is no proxy, we can send email easily using system.net.mail namespace. Can u tell me how to use proxy when sending email. My prosy host is 192.168.0.11 with port: 3370.
Channel whitePosted Nov 21, 2007, 1:16 PM
Thank you for helping me bypass this crazy ass proxys that dont hace shit to do wit what iam doing Thanks...........................
Gerbrand ColombierPosted Nov 7, 2007, 11:15 AM
Hi, If i have an url like: https://username:[email protected]/.... how do i program it in c#? I tried with httpwebrequest but i get an error back that i'm not authorized this link should give me an xml file or txt file thanks
Darshan PatelPosted Sep 8, 2007, 8:45 PM
Is it possible to download an XML file from server to the client machine using Javascript..if yes then how???? Please Reply