How to retrieve a file
from the Web through a proxy server.
Download
Simple HTTP Reader sample project (wsc_http_reader_03.zip, 5KB)
In the two previous tutorials we have learned how to communicate with
a web server via direct connection. In that scenario the client
application establishes connection to the web server by the specified
server address (or IP address) and IP-port 80. But there are a lot of
cases where a client has an access to the web only via a proxy server.
These two different types of the connection demand different ways of
implementation.
If the client application may communicate with the web only via the
proxy server, it must to know an address of the proxy server and the
port number to connect to. Thus it must allow user to input such
information that usually can be obtained from the local network
administrator.
Let's look at how this feature is implemented in the Internet
Explorer. Invoke the "Internet Options" dialog window, select the
"Connections" tab and click the "LAN Settings…" button to show the
following dialog window:

As you see, to force the IE to work via a proxy server, the user must
turn on the "Use a proxy server" checkbox and specify address and port
of the proxy server.
Now let's add the similar functionality to our Simple HTTP Reader
sample application. Place six new controls on the form as shown
below:

| Control |
Name |
Text/Caption property |
| Frame |
Frame1 |
|
| CheckBox |
chkUseProxy |
Use Proxy |
| Label |
Label2 |
Proxy Server: |
| Label |
Label3 |
Port: |
| TextBox |
txtProxyServer |
txtProxyServer |
| TextBox |
txtProxyPort |
txtProxyPort |
Open the code editor for the form and add the following event
procedure:
Private Sub
chkUseProxy_Click()
'
Dim
blnEnabled As Boolean
'
blnEnabled =
Check1.Value
'
Label2.Enabled =
blnEnabled
Label3.Enabled = blnEnabled
txtProxyServer.Enabled = blnEnabled
txtProxyPort.Enabled
= blnEnabled
'
End Sub
In the Form_Load event procedure type these two lines of code, to
clear textboxes in the run time:
txtProxyPort = ""
txtProxyServer = ""
The last thing, which we need to make - modify the cmdReadURL_Click
event procedure. But before to write some code, let's consider the way
of the communication with a proxy server. The principle is very simple.
Our application must verify the state of the chkUseProxy checkbox and if
the checkbox turned on, read the values of the txtProxyServer and the
txtProxyPort textboxes to use them to establish a connection with the
proxy server. For example:
If chkUseProxy.Value Then
wscHttp.Connect
txtProxyServer, txtProxyPort
End If
One more thing we need to perform is to change the form of the HTTP
request. In the case of a direct connection with a web server we sent a
relative path to the resource (file) to retrieve, but if we use a proxy
server we must specify a full URL to the resource, for example:
The HTTP request for connection via a proxy
server:
GET http://www.microsoft.com/default.asp
HTTP/1.1
Host: www.microsoft.com
Connection: close
The HTTP request for a direct
connection:
GET /default.asp HTTP/1.1
Host:
www.microsoft.com
Connection: close
That's it. No more changes. So, for retrieving a file from the Web
via a proxy server we need to:
- Establish the connection to the proxy server
- Send a FULL URL of the resource to retrieve
According to the features described above the cmdReadURL_Click event
procedure looks like one shown below:
Private Sub
cmdReadURL_Click()
'
Dim strURL As String
'temporary buffer
Dim intPort As
Integer
'
On
Error GoTo ERROR_HANDLER
'
'check the textbox
If Len(txtURL) = 0 Then
MsgBox "Please, enter the URL
to retrieve.", vbInformation
Exit
Sub
End If
'
If chkUseProxy.Value Then
'
'Using Proxy
'
If Len(txtProxyPort) > 0 And Len(txtProxyServer) > 0 Then
'
m_strRemoteHost =
txtProxyServer
intPort = CInt(txtProxyPort)
m_strFilePath = txtURL
'
If Not
Left(m_strFilePath, 7) = "http://" Then
m_strFilePath = "http://" &
m_strFilePath
End
If
'
End If
'
Else
'
'set default port number = 80
'
intPort = 80
'
'if the user has entered
"http://", remove this substring
'
If Left(txtURL, 7) =
"http://" Then
strURL =
Mid(txtURL, 8)
Else
strURL =
txtURL
End If
'
'get remote host
name
'
m_strRemoteHost =
Left$(strURL, InStr(1, strURL, "/") - 1)
'
'get relative path to the
file to retrieve
'
m_strFilePath
= Mid$(strURL, InStr(1, strURL, "/"))
'
End
If
'
'clear the
RichTextBox
'
rtbDocument.Text = ""
'
'clear the buffer
'
m_strHttpResponse =
""
'
'turn off the m_bResponseReceived
flag
'
m_bResponseReceived = False
'
'reset values of
the module level variables
'
m_bHeaderReceived = False
m_lContentLength = 0
m_lDownloadedBytes
= 0
StatusBar1.Panels(3).Text = ""
ProgressBar1.Value =
0.01
'
'establish the
connection
'
With
wscHttp
.Close
.LocalPort =
0
StatusBar1.Panels(2).Text = "Connecting to the web
server"
.Connect m_strRemoteHost, intPort
End With
'
EXIT_LABEL:
Exit
Sub
ERROR_HANDLER:
'
If Err.Number = 5
Then
strURL
= strURL & "/"
Resume 0
Else
MsgBox
"Error was occurred." & vbCrLf &
_
"Error #: " & Err.Number & vbCrLf &
_
"Description: " & Err.Description,
vbExclamation
GoTo EXIT_LABEL
End If
'
End Sub
|
Please, post here only feedback for the
article/tutorial/sample you have read above. If you have any
question and like to get an answer, post your messages in the Forum.
| |
Not functioning
Properly
Mohan Babu Kotha Wednesday, July 19, 2000
Bs
Why Friday, July 21,
2000
....
Warren Wednesday, July 26,
2000
addon
Warren Wednesday, July 26,
2000
does anyone knows how to add
authentication?
Gil Volfovitch Sunday, July 30, 2000
Possible solution for
authentication
Grant French Thursday, August 03, 2000
1
question
Mike Thursday, August 10, 2000
The authentication still not
working :-(
Gil Volfovitch Monday, August 14, 2000
This Program only Download
.txt/.html files
martin Wednesday, August 16, 2000
Authentication
tip
Frank de Groot Thursday, August 24, 2000
Storing files in
cache???
david Sunday, September 03, 2000
How to make this a dll that
can be used in asp
omjothi Saturday, September 16, 2000
Need to include Username and
Password for Proxy
Stefan Hotan Thursday, October 05, 2000
Firewall?
Neel Wednesday, October 25,
2000
Can WinSock control worked
with HTTPS ?
wilsonhau Tuesday, January 16, 2001
Using POST and Some Gabbage
in the HTML Source
Daryl Lee Tuesday, January 23, 2001
How to use this workshop with
WebBrowswe?
A. Winter Tuesday, January 23, 2001
online communication
system
siobi Thursday, February 15, 2001
How to connect FTP via Proxy
???
Harjeet Singh Tuesday, April 10, 2001
4 devices
control
papaki Wednesday, May 23, 2001
4 devices
control
papaki Wednesday, May 23, 2001
SSL
Jean Friday, June 01,
2001
SSL
problems
Aidan Sunday, July 08, 2001
Proxy Authetication Solution.
Suvigya Thursday, December 27, 2001