Web Server Authentication  
 

In some cases, it is required that the client authenticate itself to the web server prior to requesting a resource. This can be done in one of two ways, either by setting the UserName and Password properties or by providing those values to the Connect method when it is called. If you attempt to access a resource that requires authentication, you'll get the error stErrorCommandNotAuthorized.

Method Arguments

You can call the Connect method and specify the user name and password as arguments. The most common use would look like this:

Dim strUserName As String
Dim strPassword As String
Dim nError As Long

nError = HttpClient1.Connect(strHostName, , strUserName, strPassword)
If nError > 0 Then
    MsgBox HttpClient1.LastErrorString, vbExclamation
    Exit Sub
End If

In this example, the strHostName string variable contains the name of the server to connect to and strUserName and strPassword provide the credentials to authenticate the client session. Note that we omit the argument that specifies a remote port, which tells the control to use the default port number. If the function returns a value other than zero, this indicates an error and a message box is used to display the error to the user.

Property Values

Another method is to initialize the control's properties and then call the Connect method without any arguments. Here is an example of how that could be done:

Dim nError As Long

HttpClient1.HostName = Text1.Text
HttpClient1.UserName = Text2.Text
HttpClient1.Password = Text3.Text

nError = HttpClient1.Connect()
If nError > 0 Then
    MsgBox HttpClient1.LastErrorString, vbExclamation
    Exit Sub
End If

Note that the RemotePort property is not specified, which means that the default port number should be used. As with the previous example, if the connection fails then the method will return an error code and a message box is displayed to the user.