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$

sHost$="lotus411.searchdomino.com"
sObject$="411/Tips.nsf/SharewareByDate?OpenView"
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.
Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Const INTERNET_SERVICE_HTTP = 3
Const INTERNET_FLAG_RELOAD = &H80000000
Const INTERNET_DEFAULT_HTTP_PORT = 80
Const HTTP_QUERY_RAW_HEADERS_CRLF = 22

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"
Else
' 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"
Else
' 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"
Else
' 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"
Else
' prepare a buffer for the response headers, then query 'em
headerBuf$=Space$(1024)
cbHeaderBuf&=Len(headerBuf$)
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"
Else
' 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

This was first published in November 2000

Dig deeper on Domino Resources

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchWinIT

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchEnterpriseLinux

SearchVirtualDataCentre.co.UK

Close