Results 1 to 8 of 8

Thread: [RESOLVED] WinHTTP and https API

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    12

    Resolved [RESOLVED] WinHTTP and https API

    Hi,

    I have a large legacy program written in VB6, Very successfull,
    works fine on all flavours of Windows up to and including 10

    Now I need to communicate with a third party using HTML Basic Auth and
    JSON.

    SO far I have created some short code to test the communications:
    #
    Dim HttpReq As New WinHttp.WinHttpRequest

    WITH HttpReq

    .SetCredentials "<username>", "<password>", 0

    .Open "PUT", "https://<API URL>", False

    .Option(WinHttpRequestOption_SecureProtocols) = SecureProtocol_ALL
    .Option(WinHttpRequestOption_EnableRedirects) = True

    HttpReq.setRequestHeader "Content-Type", "application/json"

    .Send " "

    .Send "GET https://<API URL>"

    #

    Both the Send methods result in:
    {
    "status": "error",
    "code": 401,
    "message": "Access denied"
    }

    The sane credentials do work OK in Postman app.

    What am I missing or doing wrong please?

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: WinHTTP and https API

    SetCredentials should be called after Open.

    Since you are not sending a JSON payload (indeed, you are doing a GET so sending no payload at all) you don't have to set a Content-Type header.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    12

    Re: WinHTTP and https API

    I tried that and still get Access Denied.

    The whole code will be sending a JSON payload, once I get past this first sumbling block.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    12

    Re: WinHTTP and https API

    I tried that and still get Access Denied.

    The whole code will be sending a JSON payload, once I get past this first sumbling block.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    12

    Re: WinHTTP and https API

    Solved it!

    it is the authorisation header, should be

    .setRequestHeader "Authorization", "Basic <Base64 encoded username & password> "

    That gets a status 200 "Ok" response

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [RESOLVED] WinHTTP and https API

    You can do that, but it's a bit of a hack. If the server ever changes to require NTLM, Kerberos, Digest, etc. instead of Basic it all falls over.

    In any case just doing this as documented works just fine. This example fetches a GIF and displays it as the Form's Picture:

    Code:
        Const HTTPREQUEST_SETCREDENTIALS_FOR_SERVER As Long = 0
        Dim Vector As WIA.Vector
    
        With New WinHttp.WinHttpRequest
            .Open "GET", GIF_URL, False
            .Option(WinHttpRequestOption_SecureProtocols) = SecureProtocol_ALL
            .Option(WinHttpRequestOption_EnableRedirects) = True
            .SetCredentials USER, PW, HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
            .Send
            If 200 <= .Status And .Status < 300 Then
                Set Vector = New WIA.Vector
                Vector.BinaryData = .ResponseBody
                Set Picture = Vector.Picture
            Else
                MsgBox "Status " & CStr(.Status) & vbNewLine & vbNewLine _
                     & .StatusText
            End If
        End With

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    12

    Re: [RESOLVED] WinHTTP and https API

    Nope, tried that, back to square on with
    401
    Request Denied

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    12

    Re: [RESOLVED] WinHTTP and https API

    Nope, tried that, back to square on with
    401
    Request Denied

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width