Jump to content

WinHTTP functions


Recommended Posts

It could be something like this:

#include <WinHttp.au3>

$url = "https://slack.com/api/files.upload"
$token = "abc-123-456-abc123"
$args = "#random"
$file = "c:\Users\Public\catloloz.jpg"

; example curl command -- curl -F file=@c:\Users\Public\catloloz.jpg -F channels=#random -F token=abc-123-456-abc123 https://slack.com/api/files.upload

$sForm = _
        '<form action="' & $url & '" method="post" enctype="multipart/form-data">' & _
        ' <input type="file" name="file"/>' & _ ;
        ' <input name="channels" />' & _
        ' <input name="token" />' & _

; Initialize and get session handle
$hOpen = _WinHttpOpen()

$hConnect = $sForm ; will pass form as string so this is for coding correctness because $hConnect goes in byref

; Fill form
$sHTML = _WinHttpSimpleFormFill($hConnect, $hOpen, _
        Default, _
        "name:file", $file, _
        "name:channels", $args, _
        "name:token", $token)

If @error Then
    MsgBox(4096, "Error", "Error number = " & @error)
    ConsoleWrite("!" & $sHTML & @CRLF)

; Close handles





Link to comment
Share on other sites

  • 4 months later...
1 hour ago, samibb said:



@trancexx : Background information :)

To understand what this question is about at all, see https://www.autoitscript.com/forum/topic/203849-winhttp-invalid-server-response/?do=findComment&comment=1471590


"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

  • 1 month later...


i'm trying to use the WinHttp functions to read and write to a relay board. i have no problem for reading data with GET Method. But i can't find how to write something using the PUT method as asked in the documentation.

According to the restfulAPI documentation of the board ( https://www.moxa.com/getmedia/6b024adb-5276-429d-9837-1235ee4ac836/moxa-iologik-e1200-series-manual-v15.9.pdf )



So i haveto use the PUT method, is that working with WinHttp ?

this is actualy how i read something :

#include "json.au3"
        #include "WinHttp.au3"
        $hOpen = _WinHttpOpen() ; Initialize and get session handle
        $hConnect = _WinHttpConnect($hOpen, "") ; Get connection handle

        Local $hRequest = _WinHttpOpenRequest($hConnect, Default, "/api/slot/0/io/relay")
        _WinHttpAddRequestHeaders($hRequest, "Content-Type: application/json")
        _WinHttpAddRequestHeaders($hRequest, "Accept: vdn.dac.v1")
        $data = _WinHttpReadData($hRequest)

        Local $sHeader = _WinHttpQueryHeaders($hRequest)
        ;MsgBox(0, "Header", $sHeader)


        $object = json_decode($data)
        $relayCurrentCount = json_get($object, '[io][relay][4][relayCurrentCount]')

        msgbox ("","Result", "relayCurrentCount = " & $relayCurrentCount )


And below my PHP code, using cURL to write something with a PUT method :


$ch = curl_init(""); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Accept: vdn.dac.v1'
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, '$sPost = '{"slot":0,"io":{"relay":{"0":{"relayStatus":1}}}}');
$result = curl_exec($ch);

echo $result;


So my question is, how can i do the same with WinHTTP ??

Thank you for helping.

Edited by cetipabo
add pdf link
Link to comment
Share on other sites

Finaly i found something that works, but i don't know if this is the correct way to add the custom headers ??

$hOpen = _WinHttpOpen()
        $hConnect = _WinHttpConnect($hOpen, '')

        $sPost = '{"slot":0,"io":{"relay":{"0":{"relayStatus":1}}}}'

        $sResult = _WinHttpSimpleRequest($hConnect, "PUT", '/api/slot/0/io/relay/0/relayStatus', '', $sPost, "Accept: vdn.dac.v1" & @CRLF & "Content-Type: application/json")


Link to comment
Share on other sites

  • 10 months later...

Try to transfer the script (workable) to WinHttp UDF type always got fail, does anyone could help me

Original Script

$arb_Http = ObjCreate("winhttp.winhttprequest.5.1")
$arb_Http.Open("post", "http://test123.com", False)
$arb_Http.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
$arb_Http.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36")


$arb_Result = $arb_Http.ResponseText

MsgBox(0, "", $arb_Result)


$arb_Http = _WinHttpOpen("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36")

$arb_Connect = _WinHttpConnect($HRBS_AR_Http, "http://test123.com")
$arb_Request = _WinHttpOpenRequest($HRBS_AR_Connect, "post")

_WinHttpSendRequest($arb_Request, "Content-Type: application/x-www-form-urlencoded", "name=test_name&status=test_status")
$arb_Request = _WinHttpReadData($arb_Request)


MsgBox(0, "", $arb_Result)


Edited by jimmy123j
Link to comment
Share on other sites

  • 1 month later...


I like to do  Http Request with winHttp.au3, but for me is no function. I tried it for 3 Days i dont know what i doing wrong.

Local $sTarget = "/sites/siteXX/_api/Web/Lists/getbytitle('Helper')/items("&$sWebsiteItemId&")" & _

    Local $sSiteUrl = "https://team.xxxxx.de"

    local $sHeader = _
            'Content-Type:application/json' & @CRLF & _
            'Accept:application/json;odata=verbose' & @CRLF & _
            'X-RequestDigest:"' &  $sToken

    #cs ; in Javascript (JQuery) i will write for the request:

        var requestUrl =siteUrl + path;
         return $.ajax({
                url: requestUrl,
                type: "GET",
                cache: true,
                async: false,
                headers: {
                    "accept": "application/json;odata=verbose",
                    "content-type": "application/json;odata=verbose",

    Local $hOpen = _WinHttpOpen(Default,$WINHTTP_ACCESS_TYPE_DEFAULT_PROXY) ; use the default proy setting in win System
    local $hConnect = _WinHttpConnect($hOpen,$sSiteUrl)
    local $hRequest =_WinHttpSimpleSSLRequest($hConnect,"GET", $sTarget, Default, Default, $sHeader, Default, Default, Default, Default, 1)

#cs _WinHttpAddRequestHeaders($hRequest,"Content-Type:application/json")
    _WinHttpAddRequestHeaders($hRequest,"X-RequestDigest:" &  $sToken )
    ConsoleWrite("Response: "& $hRequest & @CRLF)
;Send the request

ConsoleWrite:  Response: 401 UNAUTHORIZED

We use am Proxy normally ? I dont now what happend. Can everybody help me? 


Link to comment
Share on other sites

  • 2 weeks later...

I find a workaround of my problem. I used another ddl ("MSXML2.XMLHTTP.6.0")  and do my own request , step by step. like:

; Simple Code without my Errorhandler 
    ; (By POST, sometime i get a com error from the ddl, but the Request was OK)
    $HttpRequest = ObjCreate("MSXML2.XMLHTTP.6.0")


    $HttpStatus = $HttpRequest.status


Link to comment
Share on other sites

  • 2 months later...

Hello everyone,

I'm trying to set some radio boxes in a web form but no way to make it works.

This is how the form looks like:

            <TR><TD><I><B> SELECT THE OPTIONS:  </B></I></TD></TR>
            <TR><TD> Model 800      </TD></TR>  <INPUT type="radio" name="model_equip" value="0"    checked>
            <TR><TD> Model 1300     </TD></TR>  <INPUT type="radio" name="model_equip" value="1"    >
            <TR><TD><I><B> Accessory one:   </B></I></TD></TR>
            <TR><TD>ENABLE      </TD></TR>  <INPUT type="radio" name="accessory_one" value="1"  >
            <TR><TD>DISABLE     </TD></TR>  <INPUT type="radio" name="accessory_one" value="0"  checked>
            <TR><TD><I><B> Accessory two:   </B></I></TD></TR>
            <TR><TD>ENABLE      </TD></TR>  <INPUT type="radio" name="accessory_two" value="1"  >
            <TR><TD>DISABLE     </TD></TR>  <INPUT type="radio" name="accessory_two" value="0"  checked>
            <BR>(BLUE CASE)
            <TR><TD><I><B> Accessory three: </B></I></TD></TR>
            <TR><TD>ENABLE      </TD></TR>  <INPUT type="radio" name="accessory_three" value="1"    >
            <TR><TD>DISABLE     </TD></TR>  <INPUT type="radio" name="accessory_three" value="0"    checked>
            <BR>(GREEN CASE)
            <INPUT type="hidden" name="PAGE_SELECT" value="21" />
            <INPUT TYPE="submit" VALUE="EXECUTE">


I copied many parts from the examples here https://github.com/dragana-r/autoit-winhttp/tree/master/WinHttp_Examples but I didn`t find any example for radio selections. This is what I tried so far:

Local $hOpen, $hConnect
   Local $sRead, $hFileHTM
   Local $hRequest

   ; Initialize and get session handle
   $hOpen = _WinHttpOpen()

   ; Get connection handle
   $hConnect = _WinHttpConnect($hOpen, "")

   ConsoleWrite(@CRLF & "************************************************************" & @CRLF)
   ; Request
   $hRequest = _WinHttpSimpleSendRequest($hConnect)     ; Gets the main page - OK
   ConsoleWrite($sRead)                                 ; Shows the mains page - OK.
   ConsoleWrite(@CRLF & "************************************************************" & @CRLF)
   ; Request
   $hRequest = _WinHttpSimpleSendRequest($hConnect, Default, "defaultform.htm")     ; Go to form - OK

   ; Simple-read...
   $sRead = _WinHttpSimpleReadData($hRequest)           ; Gets the page content - OK
   ConsoleWrite($sRead)                                 ; Shows the page content - OK
   ConsoleWrite(@CRLF & "************************************************************" & @CRLF)
   ; Fill form on this page
   ; Here I can not select the radios and I have no idea how they are identified...
   $sRead = _WinHttpSimpleFormFill($hConnect, "defaultform.htm", _
               "index:0", _
               "type:radio name:model_equip & value='0'", "true", _ ;"//input[@name='module_animal_type'][@value='1']", "checked", _
               "name:model_equip & value:0", "unchecked", _         ; Do not work!
               "name:accessory_one value:1", "checked", _           ; Do not work!
               "name:accessory_two, value:0", "true", _             ; Do not work!
               "name:accessory_three & value:0", "true"             ; Do not work!
   ConsoleWrite($sRead)                 ; Show the returned page, in this case it should returns the same form with the new selections.


Thanks in advance!


Link to comment
Share on other sites

  • 1 month later...
  • 4 weeks later...

Currently in the WinHTTP UDF when making a call to a site that requests a client certificate but does not require it fails when  WINHTTP_OPTION_CLIENT_CERT_CONTEXT is set to Null pointer. In the WinHTTP reference from Microsoft the WINHTTP_NO_CLIENT_CERT_CONTEXT macro is Null pointer and can be returned to let the requesting application know you don't have one.

This is fixed in this pull request if anyone needs this functionality. 



This issue apparent in this post and the solution presented is just to do the same DLL call that the WinHTTP UDF does under the hood but to provide the correct format for the null pointer.

Link to comment
Share on other sites

  • 2 months later...
On 7/7/2022 at 2:08 PM, Danp2 said:

@ChuckeroDid you try like this?

$sRead = _WinHttpSimpleFormFill($hConnect, "defaultform.htm", _
               "index:0", _
               "name:accessory_three", "1"


Thanks Danp2, but it's not so simple.

The web page have two controls with the same name:

<TR><TD>ENABLE      </TD></TR>  <INPUT type="radio" name="accessory_three" value="1"    >
<TR><TD>DISABLE     </TD></TR>  <INPUT type="radio" name="accessory_three" value="0"    checked>

The difference is the property "value", this is why I'm trying to identify it by a second property:

$sRead = _WinHttpSimpleFormFill($hConnect, "defaultform.htm", _
               "index:0", _
               "name:accessory_three value:1", "1"

I didn't figure out a way to identify the controls using more than one property. Identifying by only one property makes the first one be selected.


Edited by Chuckero
Link to comment
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...