Establishing a Connection | ||
In order to access a resource on a web server, it is first necessary to establish a connection. This is done by calling the Connect method and checking the return value to make sure that the connection was successful. To establish that connection, certain information is required. This includes:
HTTP servers may only be accessible through a proxy server in certain circumstances. In that case, a set of properties related to proxies must be specified before connecting. Please see the Technical Reference for further details about the ProxyHost and ProxyPort properties, as well as other information pertaining to connecting through a proxy server. To establish a connection using the HTTP control, you should call the Connect method and check the return value to ensure that the connection was successful. This can be done one of several ways, and which approach you use largely depends on personal preference and the structure of your program. Method ArgumentsYou can call the Connect method with a number of different arguments, all of which are optional. The most common use would look like this: Dim nError As Long nError = HttpClient1.Connect(strHostName) 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. 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. Note that if the resource requires authentication, then a username and password can also be passed to the method. Property ValuesAnother method is to set the host name using the control's HostName property and then calling the Connect method without any arguments. Here is an example of how that could be done: Dim nError As Long HttpClient1.HostName = "www.microsoft.com" 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. If authentication was required, the UserName and Password properties could be set as well. 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. Uniform Resource LocatorsAnyone who has used a web browser is familiar with the Uniform Resource Locator (URL); it is the value that is entered as the address of a website. URLs have a specific format which provides information about the server, the port number and the name of the resource that is being accessed:
The first part of the URL identifies the protocol, also known as the scheme, which will be used. With web servers, this will be either http or https for secure connections. If a username and password is required for authentication, then this will be included in the URL before the name of the server. Next, there is the name of the server to connect to, optionally followed by a port number. If no port number is given, then the default port for the protocol will be used. This is followed by the resource, which is usually a path to a file or script on the server. Parameters to the resource may also be specified, called the query string, which are typically used as arguments to a script that is executed on the server. A third approach to establishing a connection is to specify a URL by setting the URL property and then calling the Connect method without any arguments. By setting the URL property, the control will automatically update the various related properties such as HostName and RemotePort. Here is an example of how this could be done: Dim nError As Long HttpClient1.URL = "http://www.microsoft.com/" nError = HttpClient1.Connect() If nError > 0 Then MsgBox HttpClient1.LastErrorString, vbExclamation Exit Sub End If If you prefer to use URLs, this approach provides the simplest means of establishing the connection. It should be noted that if the URL property is assigned an invalid value, then the control will throw an exception. If you are using URLs provided by a user, it is strongly recommended that you implement an error handler, otherwise your program may terminate if the user specifies an incorrect or incomplete URL. |
||
Copyright © 2023 Catalyst Development Corporation. All rights reserved. |