Jump to content
trancexx

WinHTTP functions

Recommended Posts

Then add 

_WinHttpSetOption($hRequest, $WINHTTP_OPTION_SECURITY_FLAGS, BitOR($SECURITY_FLAG_IGNORE_UNKNOWN_CA, $SECURITY_FLAG_IGNORE_CERT_DATE_INVALID, $SECURITY_FLAG_IGNORE_CERT_CN_INVALID, $SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE))

...line before sending the request.

Share this post


Link to post
Share on other sites

@trancexx: Thank you for this wonderfull UDF :) Guessing the great potential...

WinHTTP is new to me, so maybe someone can help. I like to translate the following WGET (working) statement:

WGET.EXE --no-check-certificate --post-data "" -S -O - "https://admin:admin123@SERVER.domain.local:8463/api/invoke/GetServerInfo"

I tried this piece of code:

#include "WinHttp.au3"


Opt("MustDeclareVars", 1)


Local $hOpen = _WinHttpOpen()
Local $hConnect = _WinHttpConnect($hOpen, "https://admin:admin123@SERVER.domain.local", 8463)


Local $hRequest = _WinHttpOpenRequest($hConnect, "POST", "api/invoke/GetServerInfo")


_WinHttpSetOption($hRequest, $WINHTTP_OPTION_SECURITY_FLAGS, BitOR($SECURITY_FLAG_IGNORE_UNKNOWN_CA, $SECURITY_FLAG_IGNORE_CERT_DATE_INVALID, $SECURITY_FLAG_IGNORE_CERT_CN_INVALID, $SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE))
_WinHttpWriteData($hRequest, "")
_WinHttpSendRequest($hRequest)
_WinHttpReceiveResponse($hRequest)


Local $sHeader = _WinHttpQueryHeaders($hRequest)


_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)


MsgBox(0, "Header", $sHeader)

... without success. What I'm doing wrong?

Share this post


Link to post
Share on other sites
14 hours ago, supersonic said:

@trancexx: Thank you for this wonderfull UDF :) Guessing the great potential...

WinHTTP is new to me, so maybe someone can help. I like to translate the following WGET (working) statement:

WGET.EXE --no-check-certificate --post-data "" -S -O - "https://admin:admin123@SERVER.domain.local:8463/api/invoke/GetServerInfo"

I tried this piece of code:

#include "WinHttp.au3"


Opt("MustDeclareVars", 1)


Local $hOpen = _WinHttpOpen()
Local $hConnect = _WinHttpConnect($hOpen, "https://admin:admin123@SERVER.domain.local", 8463)


Local $hRequest = _WinHttpOpenRequest($hConnect, "POST", "api/invoke/GetServerInfo")


_WinHttpSetOption($hRequest, $WINHTTP_OPTION_SECURITY_FLAGS, BitOR($SECURITY_FLAG_IGNORE_UNKNOWN_CA, $SECURITY_FLAG_IGNORE_CERT_DATE_INVALID, $SECURITY_FLAG_IGNORE_CERT_CN_INVALID, $SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE))
_WinHttpWriteData($hRequest, "")
_WinHttpSendRequest($hRequest)
_WinHttpReceiveResponse($hRequest)


Local $sHeader = _WinHttpQueryHeaders($hRequest)


_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)


MsgBox(0, "Header", $sHeader)

... without success. What I'm doing wrong?

Try this:

#include "WinHttp.au3"


Local $hOpen = _WinHttpOpen()
Local $hConnect = _WinHttpConnect($hOpen, "https://SERVER.domain.local:8463")


Local $aRead = _WinHttpSimpleSSLRequest($hConnect, "POST", "api/invoke/GetServerInfo", Default, Default, Default, True, Default, "admin", "admin123", True)

_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

If IsArray($aRead) Then
    MsgBox(4096, "Header", $aRead[0])
Else
    MsgBox(4096 + 48, "Error", "Something is wrong!" & @CRLF & "Change something...")
EndIf

 

Share this post


Link to post
Share on other sites

Hello, this UDF looks super interesting and useful, but I have a question.

 

Is there a way to wait for a response without locking up the thread/process? (I am looking for an alternative to inetget() to download a web page in the background)

 

I see the examples and I wonder if there is a way to check every so often if the web server has responded yet, then fetch the response once it has received the data?

Not sure if this DLL supports this kind of function, but curious if it's possible with this library.

Share this post


Link to post
Share on other sites
14 hours ago, Lattis2003 said:

trancexx - do you think you will update the WinHTTP.au3 functions to include the new functions with Windows 8.x and later versions of winhttp.dll. Specifically trying to get WinHttpResetAutoProxy working.

https://msdn.microsoft.com/en-us/library/windows/desktop/hh405357(v=vs.85).aspx

Thanks

Sure. I'll add it for the next release.

Share this post


Link to post
Share on other sites

Hello all.

How to get the Remote/Local IP by _WinHttpQueryOption($hInternet, $iOption), with $iOption = $WINHTTP_OPTION_CONNECTION_INFO. I tried and not successed. 

Thanks for any help :'(

Sorry My English isn't well.

Share this post


Link to post
Share on other sites

Hello all, someone can tell me how to fix hỏi WINHTTP_CALLBACK_STATUS_FLAG_SECURITY_CHANNEL_ERROR working on Win10 but not working on Win7...

I try to request autoit.de with this code, this work on win10 perfectly but dont work on win7...

Someone can help me fix that! Thank you.

Sorry for my grammar, my english isn't good.

#include "WinHttp.au3"
Global $ServerInfo = ''
Global $hWINHTTP_STATUS_CALLBACK = DllCallbackRegister("__WINHTTP_STATUS_CALLBACK", "none", "handle;dword_ptr;dword;ptr;dword")
Global $hOpen = _WinHttpOpen()
_WinHttpSetStatusCallback($hOpen, $hWINHTTP_STATUS_CALLBACK)
Global $hConnect = _WinHttpConnect($hOpen, "autoit.de", 443)
Global $hRequest = _WinHttpOpenRequest($hConnect, 'GET', '/', '', '', '', 0x800000)

_WinHttpSetOption($hRequest, 84, 0xA8)
_WinHttpSetOption($hRequest, 63, 4)
_WinHttpSetOption($hRequest, 47, Null, 0)
_WinHttpSetOption($hRequest, 31, 0x3300)

_WinHttpSendRequest($hRequest)
_WinHttpReceiveResponse($hRequest)

MsgBox(4096, 'Response Header',  _WinHttpQueryHeaders($hRequest, 22))


_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
ConsoleWrite(@CRLF & $ServerInfo)
Sleep(2000)
DllCallbackFree($hWINHTTP_STATUS_CALLBACK)



; Define callback function
Func __WINHTTP_STATUS_CALLBACK($hInternet, $iContext, $iInternetStatus, $pStatusInformation, $iStatusInformationLength)
    #forceref $hInternet, $iContext, $pStatusInformation, $iStatusInformationLength
    Switch $iInternetStatus
;~      Case $WINHTTP_CALLBACK_STATUS_RESOLVING_NAME
;~          $ServerInfo = 'Server Name: ' & __GetDataFromPointer($pStatusInformation, $iStatusInformationLength) & @CRLF

;~      Case $WINHTTP_CALLBACK_STATUS_NAME_RESOLVED
;~          $ServerInfo &= 'Server IP: ' & __GetDataFromPointer($pStatusInformation, $iStatusInformationLength) & @CRLF

        Case $WINHTTP_CALLBACK_STATUS_SECURE_FAILURE
            Local $sStatus
            Local $aSSLError = [[0], [0x1, 'CERT_REV_FAILED'], [0x2, 'INVALID_CERT'], [0x4, 'CERT_REVOKED'], [0x8, 'INVALID_CA'], [0x10, 'CERT_CN_INVALID'], [0x20, 'CERT_DATE_INVALID'], [0x40, 'CERT_WRONG_USAGE'], [0x80000000, 'SECURITY_CHANNEL_ERROR']]
            $aSSLError[0][0] = __GetDataFromPointer($pStatusInformation, $iStatusInformationLength, 'dword')
            For $i = 1 To 8
                If BitAND($aSSLError[0][0], $aSSLError[$i][0]) = $aSSLError[$i][0] Then $sStatus &= 'SSL Fail: ' & $aSSLError[$i][1]
            Next
            MsgBox(4096, 'SSL State',  $sStatus)

    EndSwitch
EndFunc

Func __GetDataFromPointer($pInfo, $lInfo, $iReturnType = 'wchar')
    Return DllStructGetData(DllStructCreate($iReturnType & '[' & $lInfo & ']', $pInfo), 1)
EndFunc
1

 

Share this post


Link to post
Share on other sites
10 hours ago, DuyMinh93 said:

Hello all, someone can tell me how to fix hỏi WINHTTP_CALLBACK_STATUS_FLAG_SECURITY_CHANNEL_ERROR working on Win10 but not working on Win7...

I try to request autoit.de with this code, this work on win10 perfectly but dont work on win7...

Someone can help me fix that! Thank you.

Sorry for my grammar, my english isn't good.

#include "WinHttp.au3"
Global $ServerInfo = ''
Global $hWINHTTP_STATUS_CALLBACK = DllCallbackRegister("__WINHTTP_STATUS_CALLBACK", "none", "handle;dword_ptr;dword;ptr;dword")
Global $hOpen = _WinHttpOpen()
_WinHttpSetStatusCallback($hOpen, $hWINHTTP_STATUS_CALLBACK)
Global $hConnect = _WinHttpConnect($hOpen, "autoit.de", 443)
Global $hRequest = _WinHttpOpenRequest($hConnect, 'GET', '/', '', '', '', 0x800000)

_WinHttpSetOption($hRequest, 84, 0xA8)
_WinHttpSetOption($hRequest, 63, 4)
_WinHttpSetOption($hRequest, 47, Null, 0)
_WinHttpSetOption($hRequest, 31, 0x3300)

_WinHttpSendRequest($hRequest)
_WinHttpReceiveResponse($hRequest)

MsgBox(4096, 'Response Header',  _WinHttpQueryHeaders($hRequest, 22))


_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
ConsoleWrite(@CRLF & $ServerInfo)
Sleep(2000)
DllCallbackFree($hWINHTTP_STATUS_CALLBACK)



; Define callback function
Func __WINHTTP_STATUS_CALLBACK($hInternet, $iContext, $iInternetStatus, $pStatusInformation, $iStatusInformationLength)
    #forceref $hInternet, $iContext, $pStatusInformation, $iStatusInformationLength
    Switch $iInternetStatus
;~      Case $WINHTTP_CALLBACK_STATUS_RESOLVING_NAME
;~          $ServerInfo = 'Server Name: ' & __GetDataFromPointer($pStatusInformation, $iStatusInformationLength) & @CRLF

;~      Case $WINHTTP_CALLBACK_STATUS_NAME_RESOLVED
;~          $ServerInfo &= 'Server IP: ' & __GetDataFromPointer($pStatusInformation, $iStatusInformationLength) & @CRLF

        Case $WINHTTP_CALLBACK_STATUS_SECURE_FAILURE
            Local $sStatus
            Local $aSSLError = [[0], [0x1, 'CERT_REV_FAILED'], [0x2, 'INVALID_CERT'], [0x4, 'CERT_REVOKED'], [0x8, 'INVALID_CA'], [0x10, 'CERT_CN_INVALID'], [0x20, 'CERT_DATE_INVALID'], [0x40, 'CERT_WRONG_USAGE'], [0x80000000, 'SECURITY_CHANNEL_ERROR']]
            $aSSLError[0][0] = __GetDataFromPointer($pStatusInformation, $iStatusInformationLength, 'dword')
            For $i = 1 To 8
                If BitAND($aSSLError[0][0], $aSSLError[$i][0]) = $aSSLError[$i][0] Then $sStatus &= 'SSL Fail: ' & $aSSLError[$i][1]
            Next
            MsgBox(4096, 'SSL State',  $sStatus)

    EndSwitch
EndFunc

Func __GetDataFromPointer($pInfo, $lInfo, $iReturnType = 'wchar')
    Return DllStructGetData(DllStructCreate($iReturnType & '[' & $lInfo & ']', $pInfo), 1)
EndFunc
1

 

What exactly is the problem? Autoit.de has valid certificate. 

Also, can you use constants instead of magic numbers when showing the code meant for debugging? It's kinda hard to read thru it this way.

Share this post


Link to post
Share on other sites
7 hours ago, trancexx said:

What exactly is the problem? Autoit.de has valid certificate. 

Also, can you use constants instead of magic numbers when showing the code meant for debugging? It's kinda hard to read thru it this way.

Sorry for that, I want to know how to request autoit.de with winhttp on Windows7... Can you help me?

Thank you... My english isnt good so i cant explain exactly my question... Sorry for that again...

Share this post


Link to post
Share on other sites
8 hours ago, DuyMinh93 said:

Sorry for that, I want to know how to request autoit.de with winhttp on Windows7... Can you help me?

Thank you... My english isnt good so i cant explain exactly my question... Sorry for that again...

Ok, once again... What is the problem?

Share this post


Link to post
Share on other sites
49 minutes ago, trancexx said:

Ok, once again... What is the problem?

Thanks for your replay...

I want to get source from autoit.de but when i use _winhttpsendrequest, I receive nothing, then i use callback to trap the error, I see $WINHTTP_CALLBACK_STATUS_FLAG_SECURITY_CHANNEL_ERROR. (I use Windows7)

 I try request autoit.de on win10, it working but it dont working on win7... 

so my question is I want to know the reason why I can't request autoit.de on windows7 and how to fix it...

Sorry cuz waste your time... :(

Share this post


Link to post
Share on other sites
2 hours ago, DuyMinh93 said:

Thanks for your replay...

I want to get source from autoit.de but when i use _winhttpsendrequest, I receive nothing, then i use callback to trap the error, I see $WINHTTP_CALLBACK_STATUS_FLAG_SECURITY_CHANNEL_ERROR. (I use Windows7)

 I try request autoit.de on win10, it working but it dont working on win7... 

so my question is I want to know the reason why I can't request autoit.de on windows7 and how to fix it...

Sorry cuz waste your time... :(

You should probably check if that system is missing some components. The error you get suggests something is wrong on lower level.

Share this post


Link to post
Share on other sites

Hi everyone

Hi @trancexx

I need add a CERT to the request handle. And I've found this code (C++)

hint = InternetOpen("WebTestClient",INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
if(hint!=NULL)
{
    InternetSetStatusCallback(hint,&HTTPStatusCallbackFunc);
    hsession = InternetConnect(hint,"127.0.0.1",443,NULL,NULL,3,0,NULL);
    if(hsession!=NULL)
    {
        hreqest = HttpOpenRequest(hsession,"GET","/WebShop/","HTTP/1.1",NULL,(LPCSTR*)types,INTERNET_FLAG_SECURE|INTERNET_FLAG_IGNORE_CERT_CN_INVALID,NULL);
        if(hreqest!=NULL)
        {
            if(SetupSSL(hreqest))
            {
                if(HttpSendRequest(hreqest,NULL,0,inbuf,inlen))
                {
                    ...
                }
            }
            InternetCloseHandle(hreqest);
        }
        InternetCloseHandle(hsession);
    }
    InternetCloseHandle(hint);
}

bool SetupSSL(HINTERNET request)
{
    HCERTSTORE store = CertOpenSystemStore(NULL,"MY");
    DWORD ret = 0;
    bool ok = false;

    if(store==NULL)
        return false;
    PCCERT_CONTEXT context = CertFindCertificateInStore(store,X509_ASN_ENCODING,0,CERT_FIND_SUBJECT_STR,L"WebClient",NULL);
    if(context!=NULL)
    {
        // this fails
        ok = WinHttpSetOption(request,WINHTTP_OPTION_CLIENT_CERT_CONTEXT,(LPVOID)context,sizeof(CERT_CONTEXT))==TRUE;
        if(!ok)
            ret = GetLastError();  // returns 6
        CertFreeCertificateContext(context);
    }
    CertCloseStore(store,0);
    return ok;
};

 

I have no experience on C++. Can anybody help me convert this code to AutoIt. Many and many thanks.

Edited by moimon

Share this post


Link to post
Share on other sites

Needing some clarification.  I am using _winhttpsimplesslrequest with post.  the requirements of the api state that I must use a json array in the body tags of my request.  I thought this was  $sdta of the function but I am not getting the expected result.  I am getting a 500 error.  Could this be because I am not actually sending a JSON array?  Or am I missing a function that I should be using to input the data into the body.

;solarwinds.au3
#Region Includes
#include-once 
#include <log4a.au3>
#include <Array.au3>
#include <WinHttp.au3>
#EndRegion
    
#Region Logging
    ; Enable logging and don't write to stderr
    _log4a_SetEnable()
    _log4a_SetMinLevel($LOG4A_LEVEL_TRACE);Possible levels: $LOG4A_LEVEL_TRACE
;                                                           $LOG4A_LEVEL_DEBUG
;                                                           $LOG4A_LEVEL_INFO
;                                                           $LOG4A_LEVEL_WARN
;                                                           $LOG4A_LEVEL_ERROR
;                                                           $LOG4A_LEVEL_FATAL
    _log4a_SetErrorStream(False)
    _log4a_SetFormat("${shortdate} | ${host} | ${level} | ${message}")
    _log4a_SetLogFile(@ScriptFullPath & ".log");Sets the path of the log file (Default: "<ScriptFullPath>.log")
    _log4a_SetCompiledOutput($LOG4A_OUTPUT_BOTH);Possible outputs:  $LOG4A_OUTPUT_CONSOLE = Direct output to the console
;                                                                   $LOG4A_OUTPUT_FILE = Direct output to the log file.
;                                                                   $LOG4A_OUTPUT_BOTH = Direct output to both the console and the log file.
#EndRegion

Global $debug =0
Global $host = "usandl0213"
Global $hOpen,$hConnect,$hRequest


_orionConnect($host)
$response = _orionNew()
 _orionDisconnect()




Func _orionConnect($orion_host)
    $hOpen = _winhttpOpen()
    If @error Then
        _log4a_Fatal("Error intializing the usage of WinHTTP functions")
        Exit 1
    EndIf
    $hConnect = _winhttpConnect($hOpen, $orion_host,"17778")
    If @error Then 
        _log4a_Fatal("Error specifying the initial target server of an HTTP request.") 
        _orionDisconnect() 
        Exit 2 
    EndIf 
EndFunc

Func _orionNew()
    Local $sHeader = _
    'Authorization: Basic YWRtaW46, '  & _
    'Accept: */*, '  & _
    'User-Agent: curl/7.20.0 (i386-pc-win32) libcurl/7.20.0 OpenSSL/0.9.8l zlib/1.2.3, '  & _
    'Host: usandl0213:17778, '  & _
    'Content-Type: application/json '
    _log4a_trace($sHeader)
    
    Local $sDta = '{'  & _
    '"EntityType":"Orion.Nodes", '  & _
    '"IPAddress":"127.0.0.1", '  & _
    '"Caption":"Test - Test", '  & _
    '"DynamicIP": false, '  & _
    '"EngineID": 1, '  & _
    '"Status": 1, '  & _
    '"UnManaged": false, '  & _
    '"Allow64BitCounters": false, '  & _
    '"SysObjectID": "", '  & _
    '"MachineType": "", '  & _
    '"VendorIcon": "", '  & _
    '"ObjectSubType": "SNMP", '  & _
    '"SNMPVersion": 2, '  & _
    '"Community":"public"'  & _
    '}'
    _log4a_trace($sDta)

    
    Local $orion_QueryResponse =  _WinHttpSimpleSSLRequest($hConnect,"Post"  ,"/SolarWinds/InformationService/v3/Json/Create/Orion.Nodes",Default    ,   $sDta,  $sHeader, True        ,  Default,    Default,    Default,  1)
;~                                _WinHttpSimpleSSLRequest($hConnect, $sType, $sPath                                                     , $sReferrer,   $sDta,  $sHeader, $fGetHeaders,   $iMode, $sCredName, $sCredPass, $iIgnoreCertErrors)
    If @error Then 
        Switch @error
            Case 1
                _log4a_Fatal("could not open request.") 
            Case 2
                _log4a_Fatal("could not send request.") 
            Case 3
                _log4a_Fatal("could not receive response.") 
            Case 4
                _log4a_Fatal("$iMode is not valid.") 
        EndSwitch
    EndIf
    _log4a_trace(@crlf & $orion_QueryResponse[0])
    _log4a_trace($orion_QueryResponse[1])
    _log4a_trace($orion_QueryResponse[2])
    if $debug = 1 then  _arraydisplay($orion_QueryResponse)
    return $orion_QueryResponse
EndFunc

Func _orionDisconnect()
    _WinHttpCloseHandle($hRequest) 
    _WinHttpCloseHandle($hConnect) 
    _WinHttpCloseHandle($hOpen)
EndFunc

If I use a Rest client here are my results:

POST /SolarWinds/InformationService/v3/Json/Create/Orion.Nodes HTTP/1.1
Authorization: Basic YWRtaW46
Accept: */*
User-Agent: curl/7.20.0 (i386-pc-win32) libcurl/7.20.0 OpenSSL/0.9.8l zlib/1.2.3
Content-Length: 296
Host: usandl0213:17778
Content-Type: application/json

{"EntityType":"Orion.Nodes", "IPAddress":"127.0.0.1", "Caption":"Test - Test", "DynamicIP": false, "EngineID": 1, "Status": 1, "UnManaged": false, "Allow64BitCounters": false, "SysObjectID": "", "MachineType": "", "VendorIcon": "", "ObjectSubType": "SNMP", "SNMPVersion": 2, "Community":"public"}



HTTP/1.1 200 OK
Content-Length: 60
Content-Type: application/json
Server: Microsoft-HTTPAPI/2.0
Date: Fri, 25 Aug 2017 16:27:39 GMT

"swis://USANDL0213.nestle.com/Orion/Orion.Nodes/NodeID=1287"

 

Share this post


Link to post
Share on other sites
On ‎25‎.‎8‎.‎2017‎. at 9:20 PM, nhardel said:

Needing some clarification.  I am using _winhttpsimplesslrequest with post.  the requirements of the api state that I must use a json array in the body tags of my request.  I thought this was  $sdta of the function but I am not getting the expected result.  I am getting a 500 error.  Could this be because I am not actually sending a JSON array?  Or am I missing a function that I should be using to input the data into the body.

;solarwinds.au3
#Region Includes
#include-once 
#include <log4a.au3>
#include <Array.au3>
#include <WinHttp.au3>
#EndRegion
    
#Region Logging
    ; Enable logging and don't write to stderr
    _log4a_SetEnable()
    _log4a_SetMinLevel($LOG4A_LEVEL_TRACE);Possible levels: $LOG4A_LEVEL_TRACE
;                                                           $LOG4A_LEVEL_DEBUG
;                                                           $LOG4A_LEVEL_INFO
;                                                           $LOG4A_LEVEL_WARN
;                                                           $LOG4A_LEVEL_ERROR
;                                                           $LOG4A_LEVEL_FATAL
    _log4a_SetErrorStream(False)
    _log4a_SetFormat("${shortdate} | ${host} | ${level} | ${message}")
    _log4a_SetLogFile(@ScriptFullPath & ".log");Sets the path of the log file (Default: "<ScriptFullPath>.log")
    _log4a_SetCompiledOutput($LOG4A_OUTPUT_BOTH);Possible outputs:  $LOG4A_OUTPUT_CONSOLE = Direct output to the console
;                                                                   $LOG4A_OUTPUT_FILE = Direct output to the log file.
;                                                                   $LOG4A_OUTPUT_BOTH = Direct output to both the console and the log file.
#EndRegion

Global $debug =0
Global $host = "usandl0213"
Global $hOpen,$hConnect,$hRequest


_orionConnect($host)
$response = _orionNew()
 _orionDisconnect()




Func _orionConnect($orion_host)
    $hOpen = _winhttpOpen()
    If @error Then
        _log4a_Fatal("Error intializing the usage of WinHTTP functions")
        Exit 1
    EndIf
    $hConnect = _winhttpConnect($hOpen, $orion_host,"17778")
    If @error Then 
        _log4a_Fatal("Error specifying the initial target server of an HTTP request.") 
        _orionDisconnect() 
        Exit 2 
    EndIf 
EndFunc

Func _orionNew()
    Local $sHeader = _
    'Authorization: Basic YWRtaW46, '  & _
    'Accept: */*, '  & _
    'User-Agent: curl/7.20.0 (i386-pc-win32) libcurl/7.20.0 OpenSSL/0.9.8l zlib/1.2.3, '  & _
    'Host: usandl0213:17778, '  & _
    'Content-Type: application/json '
    _log4a_trace($sHeader)
    
    Local $sDta = '{'  & _
    '"EntityType":"Orion.Nodes", '  & _
    '"IPAddress":"127.0.0.1", '  & _
    '"Caption":"Test - Test", '  & _
    '"DynamicIP": false, '  & _
    '"EngineID": 1, '  & _
    '"Status": 1, '  & _
    '"UnManaged": false, '  & _
    '"Allow64BitCounters": false, '  & _
    '"SysObjectID": "", '  & _
    '"MachineType": "", '  & _
    '"VendorIcon": "", '  & _
    '"ObjectSubType": "SNMP", '  & _
    '"SNMPVersion": 2, '  & _
    '"Community":"public"'  & _
    '}'
    _log4a_trace($sDta)

    
    Local $orion_QueryResponse =  _WinHttpSimpleSSLRequest($hConnect,"Post"  ,"/SolarWinds/InformationService/v3/Json/Create/Orion.Nodes",Default    ,   $sDta,  $sHeader, True        ,  Default,    Default,    Default,  1)
;~                                _WinHttpSimpleSSLRequest($hConnect, $sType, $sPath                                                     , $sReferrer,   $sDta,  $sHeader, $fGetHeaders,   $iMode, $sCredName, $sCredPass, $iIgnoreCertErrors)
    If @error Then 
        Switch @error
            Case 1
                _log4a_Fatal("could not open request.") 
            Case 2
                _log4a_Fatal("could not send request.") 
            Case 3
                _log4a_Fatal("could not receive response.") 
            Case 4
                _log4a_Fatal("$iMode is not valid.") 
        EndSwitch
    EndIf
    _log4a_trace(@crlf & $orion_QueryResponse[0])
    _log4a_trace($orion_QueryResponse[1])
    _log4a_trace($orion_QueryResponse[2])
    if $debug = 1 then  _arraydisplay($orion_QueryResponse)
    return $orion_QueryResponse
EndFunc

Func _orionDisconnect()
    _WinHttpCloseHandle($hRequest) 
    _WinHttpCloseHandle($hConnect) 
    _WinHttpCloseHandle($hOpen)
EndFunc

If I use a Rest client here are my results:

POST /SolarWinds/InformationService/v3/Json/Create/Orion.Nodes HTTP/1.1
Authorization: Basic YWRtaW46
Accept: */*
User-Agent: curl/7.20.0 (i386-pc-win32) libcurl/7.20.0 OpenSSL/0.9.8l zlib/1.2.3
Content-Length: 296
Host: usandl0213:17778
Content-Type: application/json

{"EntityType":"Orion.Nodes", "IPAddress":"127.0.0.1", "Caption":"Test - Test", "DynamicIP": false, "EngineID": 1, "Status": 1, "UnManaged": false, "Allow64BitCounters": false, "SysObjectID": "", "MachineType": "", "VendorIcon": "", "ObjectSubType": "SNMP", "SNMPVersion": 2, "Community":"public"}



HTTP/1.1 200 OK
Content-Length: 60
Content-Type: application/json
Server: Microsoft-HTTPAPI/2.0
Date: Fri, 25 Aug 2017 16:27:39 GMT

"swis://USANDL0213.nestle.com/Orion/Orion.Nodes/NodeID=1287"

 

Header you send is a mess. Each part should be separated with line feed. Like this maybe:

Local $sHeader = _
            'Authorization: Basic YWRtaW46' & @CRLF & _
            'Accept: */*' & @CRLF & _
            'User-Agent: curl/7.20.0 (i386-pc-win32) libcurl/7.20.0 OpenSSL/0.9.8l zlib/1.2.3' & @CRLF & _
            'Content-Type: application/json'

 

...I won't confuse you with comments about your code at this point.

Share this post


Link to post
Share on other sites

Okay, so I cleaned up the header as @trancexx suggested.  That did help.  I now get the following response.

<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid Hostname</h2>
<hr><p>HTTP Error 400. The request hostname is invalid.</p>
</BODY></HTML>

So that makes me assume that my I do need to push json array with my request.  So I need a json udf.  Here are my options:

JSON_UDF Library (fully RFC4627-compliant) by Gabriel13

&

A Non-StrictJSON &nbsp;UDF (JSMN) by Ward

 

I am currently looking into the UDF by Gabriel13.  It however seems very outdated and not much support on that thread if I do run into problems.  Maybe it is just that easy, so don't flame me yet.  I am looking into it.  The second one by Ward seems to be the better option but by all accounts looks to be a dead project.  The first post has all dead download links.  Hoping someone might have a copy or could point me to something else that would work.  No real question here, just looking for thoughts or ideas.  Or the occasional "hey dumbass, the answer is right in front of you" comments. :sweating:

Share this post


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

×
×
  • Create New...