Jump to content
trancexx

WinHTTP functions

Recommended Posts

On 3/5/2016 at 9:03 PM, trancexx said:

First thing, the UDFs inside the zip you posted don't have defined functions you use.
Second thing is why in the world you'd leave unused code inside a reproducer? What FileOpen, FileRead?!?
Third thing is I don't understand why do you constantly choose to make yourself life harder? Use _WinhttpSimpleFormFill(), and ditch JSON construction shit.

Try this ( just set correct access token):

#include "WinHttp.au3"

$sAccessToken = "a.2g3Ymc00aAGgibbbbQDvqnX7u5p3TG34" ; write yours here
$sIden = ""; to whatever device you want

$hOpen = _WinHttpOpen()

; PUSH!!!!!
$sAddress = "https://api.pushbullet.com/v2/pushes"
$sForm = _
        '<form action="' & $sAddress & '" method="post" enctype="multipart/form-data">' & _
        ' <input name="device_iden" />' & _
        ' <input name="type" />' & _
        ' <input name="file_name" />' & _
        ' <input name="file_type" />' & _
        ' <input name="file_url" />' & _
        ' <input name="body" />' & _
        '</form>'

$hConnect = $sForm
$sReturnedThing = _WinHttpSimpleFormFill($hConnect, $hOpen, _
        Default, _
        "name:device_iden", $sIden, _
        "name:type", "file", _
        "name:file_name", "picard-wtf.jpg", _
        "name:file_type", "image/jpg", _
        "name:file_url", "http://evolutionaryparenting.com/wp-content/uploads/2015/01/picard-wtf.jpg", _
        "name:body", "Hey!", _
        'Authorization: Bearer ' & $sAccessToken)

MsgBox(4096, "Returned", $sReturnedThing & @CRLF & "@error = " & @error & @CRLF & "@extended = " & @extended)
; Close handles
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

 

Hey

with this i can send files that are in the www how can i send files thats on my hdd??

 

can u help me?

Share this post


Link to post
Share on other sites

 

I am having issues sending a JSON string using winhttp if anyone can help me, thanks. The initial HTTP error I was getting is 400, error Bad Request. 

It should be array of destination objects",txid":"f5b74"}

 After formatting $sDta like below 

Global $sDta = '{'  & _
    '\"destination \":\"testadd99.com \", '  & _
    '\"comment \":\"Comment99 \", '  & _
    '}'

 I now get  

Response: <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Error: No default engine was specified and no extension was provided.<br> &nbsp; &nbsp;at new View (/usr/src/app/node_modules/express/lib/view.js:61:11)<br> &nbsp; &nbsp;at Function.render (/usr/src/app/node_modules/express/lib/application.js:570:12)<br> &nbsp; &nbsp;at ServerResponse.render (/usr/src/app/node_modules/express/lib/response.js:1008:7)<br> &nbsp; &nbsp;at /usr/src/app/dist/utils/securityheaders.js:41:13<br> &nbsp; &nbsp;at Layer.handle_error (/usr/src/app/node_modules/express/lib/router/layer.js:71:5)<br> &nbsp; &nbsp;at trim_prefix (/usr/src/app/node_modules/express/lib/router/index.js:315:13)<br> &nbsp; &nbsp;at /usr/src/app/node_modules/express/lib/router/index.js:284:7<br> &nbsp; &nbsp;at Function.process_params (/usr/src/app/node_modules/express/lib/router/index.js:335:12)<br> &nbsp; &nbsp;at next (/usr/src/app/node_modules/express/lib/router/index.js:275:10)<br> &nbsp; &nbsp;at Layer.handle_error (/usr/src/app/node_modules/express/lib/router/layer.js:67:12)</pre>
</body>
</html>

Here is the code I am using.

#include <WinHttp.au3>

Global $Domain = "https://management.api.umbrella.com/"

Global $sTarget = "v1/organizations/12345/destinationlists/54321/destinations"

Global $sHeader = _
            'Authorization: Basic XYZ' & @CRLF & _
            'Accept: application/json' & @CRLF & _
            'Content-Type: application/json'

Global $sDta = '{'  & _
    '\"destination \":\"testadd99.com \", '  & _
    '\"comment \":\"Comment99 \", '  & _
    '}'

Global $hOpen = _winhttpOpen()
If @error Then
    Exit 1
EndIf

Global $hConnect = _winhttpConnect($hOpen, $Domain)
If @error Then
    _WinHttpCloseHandle($hOpen)
    Exit 2
EndIf

Global $hRequest = _WinHttpSimpleSSLRequest($hConnect,"POST", $sTarget, Default, $sDta, $sHeader, Default, Default, Default, Default, 1)

ConsoleWrite("Response: "& $hRequest & @CRLF)

If @error Then
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hOpen)
    Exit 3
EndIf

;Send the request
_WinHttpSendRequest($hRequest)

API documentation, https://docs.umbrella.com/umbrella-api/reference#post_v1-organizations-organizationid-destinationlists-destinationlistid-destinations

image.png

Share this post


Link to post
Share on other sites
Posted (edited)

ok, I found that this one line string works

Global $sDta = '[{"destination":"testing99.com","comment":"COMMENT99"}]'

Now I need to figure out how to replcae tesing99.com and COMMENT99 with $varibles.

Fixed it, posting answer as it may help somone else.

$URL = "testing1003.com"
$Comment = "Comment1003"

Global $sDta = '[{"destination":"' & $URL & _
               '","comment":"' & $Comment & _
               '"}]'

 

Edited by Remo_

Share this post


Link to post
Share on other sites

Tried that, :). Here is the UDF, under $Type, I am only seeing GET or POST, no DELETE.

 

; #FUNCTION# ====================================================================================================================
; Name...........: _WinHttpSimpleSSLRequest
; Description ...: A function to send a SSL request in a simpler form
; Syntax.........: _WinHttpSimpleSSLRequest($hConnect [, $sType [, $sPath [, $sReferrer = Default [, $sDta = Default [, $sHeader = Default [, $fGetHeaders = Default [, $iMode = Default ]]]]]]])
; Parameters ....: $hConnect  - Handle from _WinHttpConnect
;                  $sType       - [optional] GET or POST (default: GET)
;                  $sPath       - [optional] request path (default: "" - empty string; meaning 'default' page on the server)
;                  $sReferrer   - [optional] referrer (default: $WINHTTP_NO_REFERER)
;                  $sDta        - [optional] POST-Data (default: $WINHTTP_NO_REQUEST_DATA)
;                  $sHeader     - [optional] additional Headers (default: $WINHTTP_NO_ADDITIONAL_HEADERS)
;                  $fGetHeaders - [optional] return response headers (default: False)
;                  $iMode       - [optional] reading mode of result
;                  |0 - ASCII-text
;                  |1 - UTF-8 text
;                  |2 - binary data

image.png.e87279ffeb3d4d98ed72a2f10bef7843.png

 

Edited by Remo_

Share this post


Link to post
Share on other sites

Hey @trancexx, first of all thank you and @ProgAndy for this great UDF!

But isn't "_WinHttpSimpleSSLRequest" function description in the help file incomplete? It reads:

_WinHttpSimpleSSLRequest($hConnect [, $sType [, $sPath [, $sReferrer = Default [, $sDta = Default [, $sHeader = Default [, $fGetHeaders = Default [, $iMode = Default ]]]]]]])

Shouldn't it be:

_WinHttpSimpleSSLRequest($hConnect [, $sType [, $sPath [, $sReferrer = Default [, $sDta = Default [, $sHeader = Default [, $fGetHeaders = Default [, $iMode = Default [, $sCredName = Default [, $sCredPass = Default [, $iIgnoreCertErrors = 0 ]]]]]]]]]]])

Here is an example if you want to include it inside the help file as well:

#include "WinHttp.au3"
#include <Array.au3>

Opt("MustDeclareVars", 1)

; My server
Global $sLocalIP = "127.0.0.1"
Global $sPort = "58458"
Global $sUrl = "/url/to/requested/page/"
Global $sLogin = "admin"
Global $sPassword = "adminpassword"
; Initialize and get session handle
Global $hOpen = _WinHttpOpen()
; Get connection handle
Global $hConnect = _WinHttpConnect($hOpen, $sLocalIP, $sPort)
; Specify the reguest
Global $hRequest = _WinHttpSimpleSSLRequest($hConnect,"GET", $sUrl, Default, Default, Default, True , Default, $sLogin, $sPassword, 1)

if IsArray($hRequest) then _arraydisplay($hRequest)

; Close handles
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

It might also be good to add in the "_WinHttpSetCredentials" function help file that for https credentials should be set using _WinHttpSimpleSendSSLRequest if that's the case.

Thanks :)

 

Edited by Neutro

Share this post


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

Hey @trancexx, first of all thank you and @ProgAndy for this great UDF!

But isn't "_WinHttpSimpleSSLRequest" function description in the help file incomplete? It reads:

_WinHttpSimpleSSLRequest($hConnect [, $sType [, $sPath [, $sReferrer = Default [, $sDta = Default [, $sHeader = Default [, $fGetHeaders = Default [, $iMode = Default ]]]]]]])

Shouldn't it be:

_WinHttpSimpleSSLRequest($hConnect [, $sType [, $sPath [, $sReferrer = Default [, $sDta = Default [, $sHeader = Default [, $fGetHeaders = Default [, $iMode = Default [, $sCredName = Default [, $sCredPass = Default [, $iIgnoreCertErrors = 0 ]]]]]]]]]]])

Here is an example if you want to include it inside the help file as well:

#include "WinHttp.au3"
#include <Array.au3>

Opt("MustDeclareVars", 1)

; My server
Global $sLocalIP = "127.0.0.1"
Global $sPort = "58458"
Global $sUrl = "/url/to/requested/page/"
Global $sLogin = "admin"
Global $sPassword = "adminpassword"
; Initialize and get session handle
Global $hOpen = _WinHttpOpen()
; Get connection handle
Global $hConnect = _WinHttpConnect($hOpen, $sLocalIP, $sPort)
; Specify the reguest
Global $hRequest = _WinHttpSimpleSSLRequest($hConnect,"GET", $sUrl, Default, Default, Default, True , Default, $sLogin, $sPassword, 1)

if IsArray($hRequest) then _arraydisplay($hRequest)

; Close handles
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

It might also be good to add in the "_WinHttpSetCredentials" function help file that for https credentials should be set using _WinHttpSimpleSendSSLRequest if that's the case.

Thanks :)

I prefer leaving last three parameters undocumented for forward compatibility reasons. They're used internally.

Share this post


Link to post
Share on other sites

Thanks for your answer :)

So this means that _WinHttpSetCredentials should be used even with https?

I've tried using it alongside _WinHttpSimpleSSLRequest with no credentials parameters but never could make it work :(

 

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...