SIGN UP MEMBER LOGIN:    
ARTICLE

Download Files using HTTP and Proxy Server in C#

Posted by Mahesh Chand Articles | Internet & Web September 21, 2004
This article explains how to pass proxy server network credentials when downloading files using HTTP.
Reader Level:

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);

Login to add your contents and source code to this article
share this article :
post comment
 

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

Posted by Gelasio Marques Oct 10, 2011

It's a good program for me.

Posted by Ahmet Kursad Cengiz Aug 27, 2011

thanks alot.

Posted by misbah alnawashi Mar 03, 2010

hi, i'm interested in ur article. i've got a project which is called remoting downloader. it enables users to download a file from the internet. but i'm confused about the remote object. in my project i'm using a class library for the remote object. i also put the webclient class into the remote object. For the client side, i'm using window form that can be given a URL of a file  on the internet. and for the server side, i'm using window form to accept the file request and download it. My problem is because i'm using httpchannel for both client and server side. but i dont know how to access the webclient class on the remote object from the server side to download it. Could u suggest me some ideas?

Posted by kevin Oct 14, 2009

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.

Posted by Pradipta Ghosh Aug 27, 2009
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor