Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_vb thread: HTTP Status of 0 when sending WinInet request


Message #1 by "Phil Allen" <phildallen@h...> on Thu, 20 Jun 2002 17:25:25
Hi,

I am trying to write a method that will make a request using SSL and 
WinInet.  It works when I try to make a standard NON-SSL request, but when 
I try to make a SSL request, I just get back a status of "0"  I am trying 
to trace the code, but I can't even figure outwhat else I can check.

Here are some specifics:

I have written an ASP listener that response correctly on my machine, but 
it does not support SSL.  I can use the method to post and recieve a reply 
from the asp page.

An outside group has sent me a java hello world applet that makes a 
request to an outside server using ssl and it works.  

When I try to make a request to the hello world, I can trace the code, and 
I just get back an HTTP status of 0.  

I would love to know if I am missing something or anyone can think of 
anything else I should check.

Here is my code
Everything seems fine, until I get to where I added a comment
saying "I get status 0 here"

--------------------------------------------------------------------------

' this method performs a GET or POST to the given URL and returns the
' response as a string by coordinating the WinInet services
Private Function RequestURL(ByVal url As String, ByVal method As String, _
                            ByVal message As String, ByVal header As 
String, _
                            ByVal port As Integer, ByVal useSSL As 
Integer) As String
    On Error GoTo errorHandler
    
    Dim openHandle As Long
    Dim connectHandle As Long
    Dim resourceHandle As Long
    Dim flags As Long
    Dim result As String
    Dim bytesRead As Long
    Dim buffer As String * 1024
    Dim statusCode As Long
    Dim statusLen As Long
    Dim dummyIndex As Long
    Dim status As Integer
    
    Dim index As Integer
    Dim serverName As String
    Dim pageName As String
    
    ' extract the port, server name, and page path / name
    ' strip the http:// if it exists
    ' If port was not speicified, we will default it
    If Left(LCase(url), 7) = "http://" Then
        url = Mid(url, 8)
        If port = 0 Then
            port = 80
        End If
    ElseIf Left(LCase(url), 8) = "https://" Then
        url = Mid(url, 9)
        If port = 0 Then
            port = 443
        End If
    ElseIf port = 0 Then
        port = 80
    End If
    
    
    index = InStr(url, "/")
    
    If index > 0 Then
        serverName = Left(url, index - 1)
        pageName = Mid(url, index + 1)
    Else
        serverName = url
        pageName = ""
    End If
    
    ' Initialize WinInet and create the Internet session
    openHandle = InternetOpen("Mozilla/4.0+(compatible;+CAT+SOAP)", 
INTERNET_OPEN_TYPE_PRECONFIG, _
            "", "", 0)
            
    
    If openHandle = 0 Then Exit Function
    
   If useSSL = 1 Then '
        flags = INTERNET_FLAG_SECURE Or 
INTERNET_FLAG_IGNORE_CERT_CN_INVALID Or 
INTERNET_FLAG_IGNORE_CERT_DATE_INVALID
   Else
       flags = 0
   End If
   
    ' establish the connection with the HTTP server
    connectHandle = InternetConnect(openHandle, serverName, port, _
            "", "", INTERNET_SERVICE_HTTP, 0, 0)
            
    If connectHandle = 0 Then
        InternetCloseHandle (openHandle)
        Exit Function
    End If
    
    ' Create the HTTP request
    resourceHandle = HttpOpenRequest(connectHandle, method, pageName, _
            "", "", "", flags, 0)
            
    If resourceHandle = 0 Then
        InternetCloseHandle (connectHandle)
        InternetCloseHandle (openHandle)
        Exit Function
    End If
    
    HttpAddRequestHeaders resourceHandle, header, -1, _
            HTTP_ADDREQ_FLAG_ADD Or HTTP_ADDREQ_FLAG_REPLACE
            
    ' keep sending until authentication is successful
    HttpSendRequest resourceHandle, "", 0, message, Len(message)
    
    statusLen = 4
    HttpQueryInfo resourceHandle, HTTP_QUERY_STATUS_CODE Or 
HTTP_QUERY_FLAG_NUMBER, _
            statusCode, statusLen, dummyIndex

'###### I get status Code 0 here    
    If statusCode <> HTTP_STATUS_OK Then
        InternetCloseHandle (resourceHandle)
        InternetCloseHandle (connectHandle)
        InternetCloseHandle (openHandle)
        
        Err.Raise vbObjectError + 512 + statusCode, "RequestURL", "HTTP 
Status: " & statusCode
    Else
        ' get the response
        bytesRead = 1
        While bytesRead
            status = InternetReadFile(resourceHandle, buffer, Len(buffer) -
 1, bytesRead)
            If status And bytesRead > 0 Then
                result = result & Left(buffer, bytesRead)
            End If
        Wend
    End If
    
    ' clean up
    InternetCloseHandle (resourceHandle)
    InternetCloseHandle (connectHandle)
    InternetCloseHandle (openHandle)
    
    RequestURL = result
    Exit Function
    
errorHandler:
    RequestURL = ""
    RaiseError g_modName, "RequestURL"
End Function

  Return to Index