Manage Learn to apply best practices and optimize your operations.

Retrieve HTTP Response Headers

If you're running on the Win32 platform, you have the ability to programmatically send an HTTP request to a Web server and inspect the headers returned from the server. This is possible via the HTTP functions available in the WinInet library (wininet.dll).

Below is a LotusScript function (getResponseHeaders) that will allow you to pass in a host to connect to, the resource you wish to inspect, as well as a username and password for accessing secure resources. As an example, here's how you could view the headers of an HTTP response from the searchDomino tips site:

Sub Click(Source As Button)

Dim sHeaders$, sHost$, sObject$

sHeaders$=getResponseHeaders(sHost$, sObject$, "", "")

Msgbox "The HTTP response headers from " & sHost$ & " are: " _
& Chr(13) & Chr(10) & Chr(13) & Chr(10) _
& sHeaders$, 0, "HTTP Response Headers"

End Sub

' (Declarations) Win32 Internet Functions Constants and Declares.

Declare Function W32_InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (Byval sAgent As String, Byval lAccessType As Long, Byval sProxyName As String, Byval sProxyBypass As String, Byval lFlags As Long) As Long
Declare Function W32_InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (Byval hInternetSession As Long, Byval sServerName As String, Byval nServerPort As Integer, Byval sUsername As String, Byval sPassword As String, Byval lService As Long, Byval lFlags As Long, Byval lContext As Long) As Long
Declare Function W32_InternetCloseHandle Lib "wininet.dll" Alias "InternetCloseHandle" (Byval hInet As Long) As Integer
Declare Function W32_HttpOpenRequest Lib "wininet.dll" Alias "HttpOpenRequestA" (Byval hHttpSession As Long, Byval sVerb As String, Byval sObjectName As String, Byval sVersion As String, Byval sReferer As String, Byval something As Long, Byval lFlags As Long, Byval lContext As Long) As Long
Declare Function W32_HttpSendRequest Lib "wininet.dll" Alias "HttpSendRequestA" (Byval hHttpRequest As Long, Byval sHeaders As String, Byval lHeadersLength As Long, sOptional As Any, Byval lOptionalLength As Long) As Integer
Declare Function W32_HttpQueryInfo Lib "wininet.dll" Alias "HttpQueryInfoA" (Byval hHttpRequest As Long, Byval lInfoLevel As Long, Byval sBuffer As String, lBufferLength As Long, lIndex As Long) As Integer

Function getResponseHeaders(sHost As String, sObject As String, sUserName As String, sPassword As String) As String

Dim rc%
Dim hSession&, hRequest&, hConnection&, cbHeaderBuf&
Dim headerBuf$

' initialize the WinInet API, retrieving any proxy or direct configuration info from the registry
hSession& = W32_InternetOpen("Mozilla/4.0 (compatible; MSIE 5.01; Windows NT)", INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0)

If hSession& = 0 Then
Msgbox "Can't initialize Win32 Internet Functions (wininet.dll)", 16, "WinInet Error"
' open an HTTP session for the site
hConnection& = W32_InternetConnect(hSession&, sHost, INTERNET_DEFAULT_HTTP_PORT, sUserName, sPassword, INTERNET_SERVICE_HTTP, 0, 0)

If hConnection& = 0 Then
Msgbox "Could not open HTTP session for site " & sHost, 16, "WinInet Error"
' create a new HTTP request handle that will retrieve a resource from the server and not cache;
' note that you can also pass a POST request to upload info (see MS doc), or send an HTTP/1.0 request instead.
hRequest& = W32_HttpOpenRequest(hConnection&, "GET", sObject, "HTTP/1.1", 0, 0, INTERNET_FLAG_RELOAD, 0)

If hRequest& = 0 Then
Msgbox "Could not generate an HTTP request handle", 16, "WinInet Error"
' send the request to the server with no additional headers (see MS doc to add your own headers)
rc%=W32_HttpSendRequest(hRequest&, "", 0, 0, 0)

If rc%=False Then
Msgbox "Could not send HTTP request", 16, "WinInet Error"
' prepare a buffer for the response headers, then query 'em
rc%=W32_HttpQueryInfo(hRequest&, HTTP_QUERY_RAW_HEADERS_CRLF, headerBuf$, cbHeaderBuf&, 0)

If rc% = 0 Then
Msgbox "Could not retrieve HTTP response headers", 16, "WinInet Error"
' return the response headers on success
getResponseHeaders=Trim$(Left$(headerBuf$, cbHeaderBuf&))
End If
End If
End If
End If
End If

' close all handles
W32_InternetCloseHandle hRequest&
W32_InternetCloseHandle hSession&
W32_InternetCloseHandle hConnection&

End Function

Dig Deeper on Domino Resources



  • Favorite iSeries cheat sheets

    Here you'll find a collection of valuable cheat sheets gathered from across the iSeries/ community. These cheat ...

  • HTML cheat sheet

    This is a really cool cheat sheet if you're looking to learn more about HTML. You'll find just about everything you every wanted ...

  • Carol Woodbury: Security

    Carol Woodbury