Jump to content

I have a problem with WinHttp..


Recommended Posts

 
why my code does not work :/

 

#include "WinHttp.au3"
#include "JSON.au3"

Opt("MustDeclareVars", 1)

Global $hOpen = _WinHttpOpen()
Global $hConnect = _WinHttpConnect($hOpen, "https://api.coinpaprika.com/", $INTERNET_DEFAULT_HTTPS_PORT)
Global $hRequest = _WinHttpOpenRequest($hConnect, _
        "GET", _
        "v1/coins/btc-bitcoin")
_WinHttpSendRequest($hRequest)
_WinHttpReceiveResponse($hRequest)
Local $sReturned = ''
If _WinHttpQueryDataAvailable($hRequest) Then
    Do
        $sReturned &= _WinHttpReadData($hRequest)
    Until @error
EndIf
MsgBox(0, 0, $sReturned)

 

Link to post
Share on other sites
11 hours ago, Celtic88 said:

why my code does not work 😕

@Celtic88

As the error message said, you were trying to send a plain HTTP request over an HTTPS port.  So you have two choices to correct the error, you can change the script to send an HTTP request over port 80 or a secure request over port 443.  Examples of the 2 options are below:

 

HTTP Request

Global $hOpen     = _WinHttpOpen()
Global $hConnect  = _WinHttpConnect($hOpen, "http://api.coinpaprika.com/")
Global $hRequest  = _WinHttpOpenRequest($hConnect, "GET", "v1/coins/btc-bitcoin")
Global $sReturned = ''

_WinHttpSendRequest($hRequest)
_WinHttpReceiveResponse($hRequest)
If _WinHttpQueryDataAvailable($hRequest) Then
    Do
        $sReturned &= _WinHttpReadData($hRequest)
    Until @error
EndIf
MsgBox(0, 0, $sReturned)

OR

HTTPS Request

Global $hOpen    = _WinHttpOpen()
Global $hConnect = _WinHttpConnect($hOpen, "https://api.coinpaprika.com/")
Global $hRequest = _WinHttpOpenRequest($hConnect, _
                                       "GET", _
                                       "v1/coins/btc-bitcoin", _
                                       "HTTP/1.1", _
                                       $WINHTTP_NO_REFERER, _
                                       "application/json", _
                                       $WINHTTP_FLAG_SECURE _
                                       )
Global $sReturned = ''

_WinHttpSendRequest($hRequest)
_WinHttpReceiveResponse($hRequest)
If _WinHttpQueryDataAvailable($hRequest) Then
    Do
        $sReturned &= _WinHttpReadData($hRequest)
    Until @error
EndIf
MsgBox(0, 0, $sReturned)

For the HTTPS request, note that the _WinHttpOpenRequest function requires the $WINHTTP_FLAG_SECURE parameter for it to send the request using the "secure" HTTPS protocol.

 

Edited by TheXman
Link to post
Share on other sites
12 hours ago, Celtic88 said:

did you try?

:wtf:     Did I try what? 

Did I try to answer your question as to why your script wasn't working?  The answer is yes

Did I show you 2 snippets of code, with context,  to further illustrate what I had written in my explanation of your issue?  The answer is yes

Did I run my examples to make sure that they worked before posting them?  The answer is yes, of course I did. 

The examples show the relevant parts of you original script, with context, that should be replaced.  They are not and were not supposed to be a fully running script.  They were snippets of code for you to compare & contrast to your original script in order for you to learn from.

I have no idea what you are asking but hopefully I have answered your question.  :huh2:  <_<

Edited by TheXman
Link to post
Share on other sites

I believe OP is on Windows 7.  I tested it and it doesn't work for me either, but works correctly on Win10.  Problem comes from _WinHttpReceiveResponse($hRequest).

By check WinAPI_GetLastError, I am receiving error 12175 which means that some protocols are not enabled.  For example TLS 1.1/1.2 are disabled by default on Win7.

For the moment, I am not pushing further the analysis of this issue.

Link to post
Share on other sites
14 minutes ago, Nine said:

I tested it and it doesn't work for me either

Is it safe to assume that by "it" you are referring to the HTTPS  script and not the HTTP script?

I tested the HTTPS & HTTP scripts on both Windows 7 & 10 and both worked for me.  However, I do have TLS 1.1 & 1.2 enabled and working on my Win 7 computers.

Edited by TheXman
Link to post
Share on other sites

Maybe the COM WinHTTP version may give a bit more error information.  It works for me but maybe the OP can run it  and see if the console messages provide more info about the specific error.

#include <Constants.au3>

http_get_example()

Func http_get_example()
    Local $oHttp = Null, $oComErr = Null

    $oComErr = ObjEvent("AutoIt.Error", com_error_handler)
    If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "Unable to register COM error handler - @error = " & @error)

    $oHttp = ObjCreate("winhttp.winhttprequest.5.1")
    If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "Unable to create HTTP COM object - @error = " & @error)

    With $oHttp
        ; Open request, set request header(s), and send request
        .Open("GET", "https://api.coinpaprika.com/v1/coins/btc-bitcoin")
        If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", StringFormat("(0x%X) %s", $oComErr.RetCode, $oComErr.WinDescription))

        .SetRequestHeader("Content-Type", "application/json")

        .Send()
        If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", StringFormat("(0x%X) %s", $oComErr.RetCode, $oComErr.Description))

        ; If request was not successful, then display error and exit
        If .Status <> 200 Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", StringFormat("HTTP Status Code = %s %s", .Status, .StatusText))

        ;Display response
        ConsoleWrite(@CRLF & "HTTP Response:" & @CRLF & .ResponseText)
    EndWith
EndFunc

Func com_error_handler($oError)
    With $oError
        ConsoleWrite(@ScriptName & " (" & .scriptline & ") : ==> COM Error intercepted !" & @CRLF)
        ConsoleWrite(@TAB & "Error Number........... " & "0x" & Hex(.number) & @CRLF)
        ConsoleWrite(@TAB & "Error WinDescription... " & StringStripWS(.windescription, $STR_STRIPTRAILING) & @CRLF)
        ConsoleWrite(@TAB & "Error Description...... " & StringStripWS(.description   , $STR_STRIPTRAILING) & @CRLF)
        ConsoleWrite(@TAB & "Error ScriptLine....... " & .scriptline & @CRLF)
        ConsoleWrite(@TAB & "Error RetCode.......... " & "0x" & Hex(.retcode) & @CRLF)
    EndWith
    Return ; Return so @error can be trapped by the calling function
EndFunc

 

Edited by TheXman
Link to post
Share on other sites

@Celtic88

If you are getting a similar error when running the COM script, then by following the instructions in the post below, you should be able to resolve the schannel error.

 

I created a new Win 7 SP1 image without the TLS updates and I was able to reproduce the schannel error.
RetCode 0x80072F7D = Error 12157 (0x2F7D)

==> COM Error intercepted !
    Error Number........... 0x80020009
    Error WinDescription... Exception occurred.
    Error Description...... An error occurred in the secure channel support
    Error ScriptLine....... 22
    Error RetCode.......... 0x80072F7D

 

I applied the windows update and created the associated registry entries and the WinHTTP and COM scripts work on the newly created Win7 VM.

 

The information on how to download the Win7 update and create the registry entries can be found here:

The standalone Microsoft Update to add the updated TLS support, in the article, still exists.  The Microsoft "Easy Fix" in the article no longer exists on the Microsoft site.  I still have a copy of the "Easy Fix" executable if you prefer to have it do the associated registry entries for you instead of manually adding them yourself.

 

Edited by TheXman
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...