locked
call web service via https RRS feed

  • Question

  • Hi all,
    I have been struggling for days to call a simple hello() web service via https in vba. Here is the server configuration
    Web server type: tomcat
    Service:  Axis2
    Openssl enabled
    keystore type: PKCS12
    password enabled
    I have confirmed that a java client can talk to hello() service via https.  
    Here is the http code works fine
    Dim clnt As New SoapClient30
    Dim strsvc As String
    Dim strwsdl As String
    clnt.ClientProperty("ServerHTTPRequest") = True
    clnt.MSSoapInit "http://localhost/axis2/services/bank?wsdl"
    clnt.ConnectorProperty("Timeout") = "9000000"
    strsvc = clnt.hello()
    MsgBox (strsvc)
    Here is HTTPS.  I think it's not completed..
    Dim swApp As Object
    Sub main()
    Dim clnt As New SoapClient30
    Dim strsvc As String
    Dim strwsdl As String
    strwsdl = "c:\Bank.wsdl"
    clnt.MSSoapInit (strwsdl)
    clnt.ConnectorProperty("SSLClientCertificateName") = "c:\cert.p12"
    clnt.ConnectorProperty("AuthPassword") = "testing"
    clnt.ConnectorProperty("Timeout") = "9000000"
    strsvc = clnt.hello()
    MsgBox (strsvc)
    Set swApp = Application.SldWorks
    End Sub
    Error
    Conector:Connection time out. HRESULT=0x800A1527 - Client:an unanticipated error occured during the processing of this request. HRESULT=0x800A1527 - client:sending the soap message failed or no recongizable response was received HRESULT=0x8001527 - Client:Unspecified client error. HRESULT=0x800A1527
    thank you all for your advice


    • Edited by tacoo07 Friday, December 9, 2011 7:29 PM
    Friday, December 9, 2011 7:25 PM

Answers

  • Try use this one UrlPost finction:

    Private Declare Function 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
    
    
    Private Declare Function 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
    
    
    Private Declare Function 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
    
    
    Private Declare Function HttpSendRequest Lib "wininet.dll" Alias "HttpSendRequestA" ( _
                     ByVal hHttpRequest As Long, _
                     ByVal sHeaders As String, _
                     ByVal lHeadersLength As Long, _
                     ByVal sOptional As String, _
                     ByVal lOptionalLength As Long) As Long
    
    
    Private Declare Function InternetReadFile Lib "wininet.dll" ( _
                     ByVal hFile As Long, _
                     ByVal sBuffer As String, _
                     ByVal lNumBytesToRead As Long, _
                     lNumberOfBytesRead As Long) As Long
    
    
    Private Declare Function HttpQueryInfo Lib "wininet.dll" Alias "HttpQueryInfoA" ( _
                     ByVal hHttpRequest As Long, _
                     ByVal lInfoLevel As Long, _
                     ByRef sBuffer As Any, _
                     ByRef lBufferLength As Long, _
                     ByRef lIndex As Long) As Integer
    
    
    Private Declare Function InternetCloseHandle Lib "wininet.dll" ( _
                        ByVal hInet As Long) As Integer
    
    
    Const INTERNET_FLAG_SECURE = &H800000
    Const INTERNET_FLAG_IGNORE_CERT_CN_I­NVALID = &H1000
    Const INTERNET_FLAG_RELOAD = &H80000000
    
    
    Const HTTP_QUERY_STATUS_CODE = 19
    Const HTTP_QUERY_STATUS_TEXT = 20
    
    
    Const INTERNET_SERVICE_HTTP = 3
    
    
    Const INTERNET_OPEN_TYPE_PRECONFIG = 0
    
    
    Public Enum INTERNET_DEF
    INTERNET_DEFAULT_HTTP_PORT = 80
    INTERNET_DEFAULT_HTTPS_PORT = 443
    End Enum
    
    Public Function UrlPost(stURL As String, stPostData As String, _
                    stStatusCode As String, stStatusText As String, _
                    Optional lgInternet As INTERNET_DEF = INTERNET_DEFAULT_HTTP_PORT, _
                    Optional stUser As String = vbNullString, _
                    Optional stPass As String = vbNullString) As String
    ' Cette fonction permet lire le contenu d'une URL.
    ' Elle permet éventuellement aussi de poster des données sur un formulaire
    ' et peut fonctionner en mode HTTP et HTTPS
    Dim stRead As String * 2048, lgRead As Long
    Dim stLoad As String
    Dim blDoLoop As Boolean
    Dim hISession As Long, hIConnect As Long, hRequest As Long
    Dim stUrlDeb As String, stUrlFin As String
    Dim stMethod As String
    Dim stPost As String
    Dim lgFlags As Long, lgRep As Long
    
    
    ' Découpage de l'URL en serveur et fichier
    If (InStr(1, stURL, "/") > 0) Then
        stUrlDeb = Replace(LCase$(stURL), "http://", vbNullString)
        stUrlDeb = Replace(LCase$(stUrlDeb), "https://", vbNullString)
        stUrlFin = stUrlDeb
        stUrlDeb = Left$(stUrlDeb, InStr(1, stUrlDeb, "/") - 1)
        stUrlFin = Mid$(stUrlFin, InStr(1, stUrlFin, "/") + 1)
    Else
        stUrlDeb = stURL
        stUrlFin = vbNullString
    End If
    ' Mise au point de la méthode d'envoi
    If (stPostData <> vbNullString) Then
        stPost = stPostData
        stMethod = "POST"
        stLoad = "Content-Type: application/x-www-form-urlenco­ded" & vbCrLf
    Else
        stPost = vbNullString
        stMethod = "GET"
        stLoad = vbNullString
    End If
    If (lgInternet = INTERNET_DEFAULT_HTTPS_PORT) Then
        lgFlags = INTERNET_FLAG_SECURE Or _
                  INTERNET_FLAG_IGNORE_CERT_CN_I­NVALID
    Else
        lgFlags = INTERNET_FLAG_RELOAD
    End If
    
    
    ' Mise en place de la connexion Internet
    hISession = InternetOpen(stUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, _
                             vbNullString, vbNullString, 0)
    If CBool(hISession) Then
        ' Ouverture de la connexion internet
        hIConnect = InternetConnect(hISession, _
                                    stUrlDeb, _
                                    lgInternet, _
                                    stUser, _
                                    stPass, _
                                    INTERNET_SERVICE_HTTP, _
                                    0, _
                                    0)
        ' Préparation de l'ouverture de la page
        hRequest = HttpOpenRequest(hIConnect, _
                                   stMethod, _
                                   stUrlFin, _
                                   "HTTP/1.0", _
                                   vbNullString, _
                                   0, _
                                   lgFlags, _
                                   0)
        ' Lancement de l'URL (avec les parametres le cas échéant)
        lgRep = HttpSendRequest(hRequest, stLoad, Len(stLoad), stPost, Len(stPost))
        ' Récupération du texte/contenu de la page
        blDoLoop = True
        stLoad = vbNullString
        Do While blDoLoop
            stRead = vbNullString
            blDoLoop = InternetReadFile(hRequest, stRead, Len(stRead), lgRead)
            stLoad = stLoad & Left$(stRead, lgRead)
            If Not CBool(lgRead) Then blDoLoop = False
        Loop
        ' Code http de retour (statut)
        stStatusCode = Space$(1024)
        lgRead = 1024
        HttpQueryInfo hRequest, HTTP_QUERY_STATUS_CODE, ByVal stStatusCode, lgRead, 0
        stStatusCode = Left$(stStatusCode, lgRead)
        ' Texte associé
        stStatusText = Space$(1024)
        lgRead = 1024
        HttpQueryInfo hRequest, HTTP_QUERY_STATUS_TEXT, ByVal stStatusText, lgRead, 0
        stStatusText = Left$(stStatusText, lgRead)
    End If
    
    
    ' Fermeture des connexions
    On Error Resume Next
    InternetCloseHandle hISession
    InternetCloseHandle hIConnect
    InternetCloseHandle hRequest
    On Error GoTo 0
    
    
    ' Retourne le contenu de la page chargée
    UrlPost = stLoad
    End Function
    



    Oskar Shon, Office System MVP

    Press if Helpful

    • Proposed as answer by danishani Saturday, January 28, 2012 3:27 AM
    • Marked as answer by danishani Monday, January 30, 2012 7:49 PM
    Wednesday, December 21, 2011 12:22 AM
    Answerer