Jump to content
trancexx

WinHTTP functions

Recommended Posts

On ‎6‎.‎2‎.‎2019‎. at 1:30 PM, HamidZaeri said:

Hi @trancexx

- How can I receive response Status? (200, 404, 403,...) Should I get it from response Header?

 

 

- And I have a problem; this request sends to this address: "http://api.server.ir:443/v2/content/"

but I want it to be sent  to "https://api.server.ir/v2/content/"

Global $hOpen = _WinHttpOpen('okhttp/3.10.0');,3,'127.0.0.1:8888')
_WinHttpSetOption($hOpen, 118, 0x00000003)
$hConnect = _WinHttpConnect($hOpen, "https://api.server.ir")
; Make a request
Global $hRequest = _WinHttpOpenRequest($hConnect, "POST", "/v2/content/")
_WinHttpAddRequestHeaders($hRequest, "Content-Type: application/x-www-form-urlencoded")

_WinHttpSendRequest($hRequest, 'Accept-Encoding: gzip', $rq[$num][0])
_WinHttpReceiveResponse($hRequest)
; Check if there is a response
If _WinHttpQueryDataAvailable($hRequest) Then
    Global $sHeader = _WinHttpQueryHeaders($hRequest), $sReturned
    MsgBox(64, "Header", $sHeader)
    Do
        $sReturned &= _WinHttpReadData($hRequest, 1)
    Until @error
    InputBox(1,ClipPut($sReturned),$sReturned)
    Exit
EndIf

.

Yes, from the response:

_WinHttpQueryHeaders($hRequest, $WINHTTP_QUERY_STATUS_CODE)

 

...I'm not sure what's the question in that second thing.

Share this post


Link to post
Share on other sites
On 2/8/2019 at 11:44 PM, trancexx said:

Yes, from the response:

_WinHttpQueryHeaders($hRequest, $WINHTTP_QUERY_STATUS_CODE)

 

...I'm not sure what's the question in that second thing.

Thanks.

Is below code good enough to get status?  Or does it need any more condition or any other better code?

If @error = 0 Then StringMid($response[0],10,3)

 

++++

And about this function: "_WinHttpSimpleSSLRequest"

What should I edit in "WinHttp" to change header parameter of requests

from This: "Accept-Encoding: gzip, deflate"

To this: "Accept-Encoding: gzip"

 

Edited by HamidZaeri

Share this post


Link to post
Share on other sites
On ‎18‎.‎2‎.‎2019‎. at 6:26 PM, HamidZaeri said:

Thanks.

Is below code good enough to get status?  Or does it need any more condition or any other better code?

If @error = 0 Then StringMid($response[0],10,3)

 

++++

And about this function: "_WinHttpSimpleSSLRequest"

What should I edit in "WinHttp" to change header parameter of requests

from This: "Accept-Encoding: gzip, deflate"

To this: "Accept-Encoding: gzip"

 

If it works then it's ok. Status line of HTTP response is standardized.

As for the other... why in the world would you want that?

 

Share this post


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

If it works then it's ok. Status line of HTTP response is standardized.

As for the other... why in the world would you want that?

 

Cause i need to simulate another request which contains only gzip!

BTW thanks for your answers, time and the great udf

Edited by HamidZaeri

Share this post


Link to post
Share on other sites

Hi

I am using inHttpWebSocket family of functions.

I call WinHttpSetStatusCallback before WinHttpWebSocketCompleteUpgrade but never get SEND and RECEIVE events from  WinHttpWebSocketReceive or HttpWebSocketSend.

I can't find any documentation for WinHttpSetStatusCallback and how it relates to WinHttpWebSocketReceive or HttpWebSocketSend.

Thoughts?

Thanks!

Share this post


Link to post
Share on other sites
Posted (edited)

Sorry, I posted the wrong topic. I hope Mod will help me remove this comment. Thank you.

Edited by moimon

Share this post


Link to post
Share on other sites

Hello, trying to send files with VirusTotal UDF, it uses WinHttp, for some reason, when file ar bigger then 8mb i can't send it, get empty response "0". Small files up to 8mb send fine. Anyone knows why ? VirusTotal accepts files up to 25mb 😕

 

Func VT(ByRef $aAPI, $Type, $sResource, $sAPIkey,$Comments="")

    If $aAPI[$eAPI_HttpConnect] = -1 Then $aAPI = VT_Open()

    Select ;$fReport,$fScan,$fRescan,$uReport,$uScan,$Comment
    Case $Type = $fReport
         Return _WinHttpSimpleRequest($aAPI[$eAPI_HttpConnect], 'POST', $tURL[$Type], Default, 'resource=' & $sResource & '&key=' & $sAPIkey)

     Case $Type = $fScan
          Local $sBoundary="--------Boundary"
          Local $sHeaders = "Content-Type: multipart/form-data; boundary=" & $sBoundary & @CRLF
          Local $sData = ''
                $sData &= "--" & $sBoundary & @CRLF
                $sData &= 'Content-Disposition: form-data; name="apikey"' & @CRLF & @CRLF & $sAPIkey & @CRLF
                $sData &= "--" & $sBoundary & @CRLF
                $sData &= __WinHttpFileContent("", "file", $sResource,$sBoundary)
                $sData &= "--" & $sBoundary & "--" & @CRLF
         Return _WinHttpSimpleRequest($aAPI[$eAPI_HttpConnect], "POST", $tURL[$Type], Default, StringToBinary($sData,0), $sHeaders)

    Case $Type = $fRescan
         Return _WinHttpSimpleRequest($aAPI[$eAPI_HttpConnect], "POST", "/vtapi/v2/file/rescan", Default, "resource=" & $sResource &"&key=" & $sAPIkey)

    Case $Type = $uReport
         Return _WinHttpSimpleRequest($aAPI[$eAPI_HttpConnect], 'POST', $tURL[$Type], Default, 'resource=' & $sResource & '&key=' & $sAPIkey)

    Case $Type = $uScan
         Return _WinHttpSimpleRequest($aAPI[$eAPI_HttpConnect], 'POST', $tURL[$Type], Default, 'url=' & $sResource & '&key=' & $sAPIkey)

    Case $Type = $Comment
         return _WinHttpSimpleRequest($aAPI[$eAPI_HttpConnect], "POST", "/vtapi/v2/comments/put", Default, "resource=" & $sResource & _
         "&comment=" & $Comments & "&key=" & $sAPIkey)

    Case Else
        SetError(3)
EndSelect

EndFunc   ;==>VT

Share this post


Link to post
Share on other sites
Posted (edited)

Hey! 

I have a problem and i tried A LOT of things but i can't make it work. I will try to explain my self as much as i can. I need to do a http request to a https server, the thing is that i do this :

$oHTTP.Open('POST', 'https://www.website.com/page.php', FALSE) ;start connection to website
$oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded") ; header
$oHTTP.Send(StringToBinary("var1=1&var2=2&var3=3", 1)) ;sending all the data

$sResponse = $oHTTP.ResponseText ;saving the response
ConsoleWrite("Response: "&$sResponse&@CRLF)

In test it in my main PC(I have a Windows 10)  and it worked and still working but when i ran this on another PC(Windows 7(installed yesterday), last Autoit updates)  i get a "compatibility error of the secure channel" error by a trap. i dont know if i need to install something that i'm missing or what. 

So i decide to test your "winhttp.au3" and i do this: 

$hOpen = _WinHttpOpen()
$hConnect = _WinHttpConnect($hOpen, "https://www.website.com") ; Connection handle

$sData = "var1=1&var2=2&var3=3" ; The data to send by POST method
$sResponse = _WinHttpSimpleSSLRequest($hConnect, "POST", "/page.php", Default, $sData) ; Make the SSL Request

_WinHttpCloseHandle($hConnect) ; closing handles
_WinHttpCloseHandle($hOpen)

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

It looks like it's working but the thing is that the $sResponse return "0" and it shouldn't be returning that, it should be returning a string with 2 numbers. I'm missing something? I need to do something more with $sResponse? it can be my PC mssing a file to do https request correctly?


The website is private so I can't make the site public, if i make it public i'm dead and I choose to live :)

Edited by jordin71

Share this post


Link to post
Share on other sites

Hi!  Please help!  I'm trying to send a Pushover notification with an attachment.  I grabbed a section of code from an earlier message in this forum, which successfully sends the notification, but I'm unable to get notification to include an attachment.  Please advise.

Pushover API: https://pushover.net/api#attachments

Here is what I grabbed from the earlier post with my failed attempt to add an attachment.

#include "WinHttp.au3"

$sAddress = "https://api.pushover.net/1/messages.json"

$sApiToken = "apitoken" ; <-yours here
$sUserKey = "userkey" ; <-yours here
$sMessage = "hello world"

; Construct the form
Const $sForm = '<form action="' & $sAddress & '" method="post"' & _
'<input name="token" value="' & $sApiToken & '"/>' & _
'<input name="user" value="' & $sUserKey & '"/>' & _
'<input name="message" value="' & $sMessage & '"/>' & _
'<input name="attachment" type="image/jpeg" filename="' & @MyDocumentsDir & "\MyPicture.jpg" & '"/>' & _
'</form>'

; Open session
$hOpen = _WinHttpOpen()
; To collect connection handle (because the form is inlined)
$hConnect = $sForm

; Fill the form
$sRead = _WinHttpSimpleFormFill($hConnect, $hOpen)

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

; Tada mtfk!
MsgBox(4096, "Simon says", $sRead)

Thanks!

Share this post


Link to post
Share on other sites
On ‎3‎/‎29‎/‎2019 at 3:49 PM, tczbu said:

Hi!  Please help!  I'm trying to send a Pushover notification with an attachment.  I grabbed a section of code from an earlier message in this forum, which successfully sends the notification, but I'm unable to get notification to include an attachment.  Please advise.

Pushover API: https://pushover.net/api#attachments

Here is what I grabbed from the earlier post with my failed attempt to add an attachment.

#include "WinHttp.au3"

$sAddress = "https://api.pushover.net/1/messages.json"

$sApiToken = "apitoken" ; <-yours here
$sUserKey = "userkey" ; <-yours here
$sMessage = "hello world"

; Construct the form
Const $sForm = '<form action="' & $sAddress & '" method="post"' & _
'<input name="token" value="' & $sApiToken & '"/>' & _
'<input name="user" value="' & $sUserKey & '"/>' & _
'<input name="message" value="' & $sMessage & '"/>' & _
'<input name="attachment" type="image/jpeg" filename="' & @MyDocumentsDir & "\MyPicture.jpg" & '"/>' & _
'</form>'

; Open session
$hOpen = _WinHttpOpen()
; To collect connection handle (because the form is inlined)
$hConnect = $sForm

; Fill the form
$sRead = _WinHttpSimpleFormFill($hConnect, $hOpen)

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

; Tada mtfk!
MsgBox(4096, "Simon says", $sRead)

Thanks!

It should be:

;...
Const $sForm = '<form action="' & $sAddress & '" method="post" enctype="multipart/form-data">' & _
'<input name="token" value="' & $sApiToken & '"/>' & _
'<input name="user" value="' & $sUserKey & '"/>' & _
'<input type="file" name="attachment"/>' & _
'<input name="message" value="' & $sMessage & '"/>' & _
'</form>'

;...
$sRead = _WinHttpSimpleFormFill($hConnect, $hOpen, Default, "name:attachment", $sImage)
;...

 

Share this post


Link to post
Share on other sites
On ‎3‎/‎29‎/‎2019 at 12:47 AM, jordin71 said:

Hey! 

I have a problem and i tried A LOT of things but i can't make it work. I will try to explain my self as much as i can. I need to do a http request to a https server, the thing is that i do this :

$oHTTP.Open('POST', 'https://www.website.com/page.php', FALSE) ;start connection to website
$oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded") ; header
$oHTTP.Send(StringToBinary("var1=1&var2=2&var3=3", 1)) ;sending all the data

$sResponse = $oHTTP.ResponseText ;saving the response
ConsoleWrite("Response: "&$sResponse&@CRLF)

In test it in my main PC(I have a Windows 10)  and it worked and still working but when i ran this on another PC(Windows 7(installed yesterday), last Autoit updates)  i get a "compatibility error of the secure channel" error by a trap. i dont know if i need to install something that i'm missing or what. 

So i decide to test your "winhttp.au3" and i do this: 

$hOpen = _WinHttpOpen()
$hConnect = _WinHttpConnect($hOpen, "https://www.website.com") ; Connection handle

$sData = "var1=1&var2=2&var3=3" ; The data to send by POST method
$sResponse = _WinHttpSimpleSSLRequest($hConnect, "POST", "/page.php", Default, $sData) ; Make the SSL Request

_WinHttpCloseHandle($hConnect) ; closing handles
_WinHttpCloseHandle($hOpen)

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

It looks like it's working but the thing is that the $sResponse return "0" and it shouldn't be returning that, it should be returning a string with 2 numbers. I'm missing something? I need to do something more with $sResponse? it can be my PC mssing a file to do https request correctly?


The website is private so I can't make the site public, if i make it public i'm dead and I choose to live :)

Try

$sResponse = _WinHttpSimpleSSLRequest($hConnect, "POST", "/page.php", Default, $sData, Default, Default, Default, Default, Default, 1) ; Make the SSL Request

 

Share this post


Link to post
Share on other sites
Posted (edited)
On 3/31/2019 at 10:52 PM, trancexx said:

Try

$sResponse = _WinHttpSimpleSSLRequest($hConnect, "POST", "/page.php", Default, $sData, Default, Default, Default, Default, Default, 1) ; Make the SSL Request

 

I tried and still getting a return of 0 :( any other suggestion?

 

If this helps $sResponse returns are these

-$sResponse : 0

-@error : 0

-@extended : 0

 

Edited by jordin71

Share this post


Link to post
Share on other sites
On 3/31/2019 at 3:39 PM, trancexx said:

It should be:

;...
Const $sForm = '<form action="' & $sAddress & '" method="post" enctype="multipart/form-data">' & _
'<input name="token" value="' & $sApiToken & '"/>' & _
'<input name="user" value="' & $sUserKey & '"/>' & _
'<input type="file" name="attachment"/>' & _
'<input name="message" value="' & $sMessage & '"/>' & _
'</form>'

;...
$sRead = _WinHttpSimpleFormFill($hConnect, $hOpen, Default, "name:attachment", $sImage)
;...

 

That worked!  Thank you!!!

Share this post


Link to post
Share on other sites

Sometimes when pulling text from a site to display in a msgbox, I get "garbage" characters that I need to replace/remove:

$sPull = _WinHttpReadData($hRequest)

        $sPull = StringRegExpReplace($sPull, "’", "'")
        $sPull= StringRegExpReplace($sPull, "€", "'")
        $sPull= StringRegExpReplace($sPull, "â€", "'")
        $sPull= StringRegExpReplace($sPull, "—", " ")
        $sPull= StringRegExpReplace($sPull, "Â", "")

Is there a way to avoid getting these characters as they don't appear in the text of the websites?
Should I be using a different function altogether instead of _WinHttpReadData()?

Thank you for your help.

Share this post


Link to post
Share on other sites
1 hour ago, coffeeturtle said:

Sometimes when pulling text from a site to display in a msgbox, I get "garbage" characters that I need to replace/remove:

$sPull = _WinHttpReadData($hRequest) 

        $sPull = StringRegExpReplace($sPull, "’", "'")
        $sPull= StringRegExpReplace($sPull, "€", "'")
        $sPull= StringRegExpReplace($sPull, "â€", "'")
        $sPull= StringRegExpReplace($sPull, "—", " ")
        $sPull= StringRegExpReplace($sPull, "Â", "")

Is there a way to avoid getting these characters as they don't appear in the text of the websites?
Should I be using a different function altogether instead of _WinHttpReadData()?

Thank you for your help.

Try  _WinHttpReadData($hRequest, 1) .

Share this post


Link to post
Share on other sites
1 hour ago, trancexx said:

Try  _WinHttpReadData($hRequest, 1) .

Thank you! Much better!

$iMode [optional] Integer representing reading mode. Default is 0 (charset is decoded as it is ANSI).

I should have played around with that.

 

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