Download Files using HTTP and Proxy Server in VB.NET

This article describes how to download a File using HTTP and Proxy Server in VB.NET.
  • 2385

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:

 

Dim result As String = ""
Try
Dim
proxy As New WebProxy(""http://proxy:80/"", True)
proxy.Credentials = New NetworkCredential("userId", "password", "Domain")
Dim request As WebRequest = WebRequest.Create("
http://www.c-sharpcorner.com")
request.Proxy = proxy
' Send the 'HttpWebRequest' and wait for response.
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
Dim stream As System.IO.Stream = response.GetResponseStream()
Dim ec As System.Text.Encoding = System.Text.Encoding.GetEncoding("utf-8")
Dim reader As New System.IO.StreamReader(stream, ec)
Dim chars() As Char = New [Char](256)
{}
Dim count As Integer = reader.Read(chars, 0, 256)
While count > 0
Dim str = New [String](chars, 0, 256)
result = result + str
count = reader.Read(chars, 0, 256)
End
While
response.Close()
stream.Close()
reader.Close()
Catch exp As Exception
Dim str As String = exp.Message
End
Try


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

 

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (WWW.C-SHARPCORNER.COM).

Categories

More Articles

© 2013 dotNetheaven. All rights reserved.