WinHttpRequest - A function similar to HttpRequest

Post your working scripts, libraries and tools for AHK v1.1 and older
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

WinHttpRequest - A function similar to HttpRequest

03 Apr 2014, 02:05

The usage is similar to HttpRequest by VxE, please visit that page for more details.

Main differences compared to HttpRequest:
  • It has a Timeout: Seconds Option.
  • Cookie
    • WinHttp startup with empty cookie, and cookies will saved automatically.
    • HttpRequest has two modes:
      1. By default, it uses Internet Explorer's cookie.
      2. When +NO_COOKIES option is specified, you need to specify Cookie: header manually.
Supported Options:
  • NO_AUTO_REDIRECT
  • Timeout: Seconds
  • Proxy: IP:Port
  • Codepage: 65001
  • Charset: utf-8
  • SaveAs: FileName
How to clear cookie:
  • Execute WinHttpRequest( [] )
Example

Code: Select all

r := WinHttpRequest("http://google.com/", InOutData := "", InOutHeaders := Headers(), "Timeout: 1`nNO_AUTO_REDIRECT")
MsgBox, % (r = -1) ? "successful" : (r = 0) ? "Timeout" : "No response"
MsgBox, % InOutData
MsgBox, % InOutHeaders
Return

Headers(referer = "")
{
	Headers =
	( LTrim
		Referer: %referer%
		User-Agent: Opera/9.80 (Windows NT 5.1) Presto/2.12.388 Version/12.16
	)
	
	Return Headers
}
:arrow: Download: gist

Note: Some old system don't have winhttp.dll, the solution is copy winhttp.dll from other machine, to c:\windows\system32\winhttp.dll, then run regsvr32 winhttp.dll.
Last edited by tmplinshi on 13 May 2015, 16:52, edited 4 times in total.
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: WinHttpRequest - A function similar to HttpRequest

03 Apr 2014, 04:30

successfully tested! :ugeek:
ozzii
Posts: 481
Joined: 30 Oct 2013, 06:04

Re: WinHttpRequest - A function similar to HttpRequest

04 Apr 2014, 02:22

Just a thought:
You can add an test for the existence of the winhttp.dll file
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: WinHttpRequest - A function similar to HttpRequest

04 Apr 2014, 02:45

@ozzii
I prefer user tell me they can't run (caused by missing winhttp.dll file), since only few systems don't have this file (e.g. Windows Server 2003 SP2).
Don_Corleon
Posts: 12
Joined: 10 Feb 2014, 21:04

Re: WinHttpRequest - A function similar to HttpRequest

06 Apr 2014, 08:40

nice work.
I will have to test it out and see if it can help to solve my ongoing http issues :)
ozzii
Posts: 481
Joined: 30 Oct 2013, 06:04

Re: WinHttpRequest - A function similar to HttpRequest

07 Apr 2014, 11:44

You misunderstood me or me you.
I didn't though to include the file but to have a check and if the file is not present to have an alert.

But it's your call.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: WinHttpRequest - A function similar to HttpRequest

07 Apr 2014, 12:00

@ozzii
Ok, got it. Thanks :)
Dave_Scream
Posts: 7
Joined: 01 Jul 2016, 06:06

Re: WinHttpRequest - A function similar to HttpRequest

01 Jul 2016, 06:12

Thank you. Today httpRequest has stopped working, dont know why maybe some update.
WinHTTPRequest works good. cyrilic codepage, file downloads everythink works good. Windows x64. WinHTTP.dll already was in my c:\Windows\System32\
skribb
Posts: 28
Joined: 22 Jun 2016, 20:52

Re: WinHttpRequest - A function similar to HttpRequest

14 Oct 2016, 11:17

Hey guys, trying to control my LIFX but I'm receiving errors (Missing "key" in object literal, and the affected line is the headers := { "Authorization": "Bearer " . token , }):

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#Include WinHttpRequest.ahk

token := "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

url := "https://api.lifx.com/v1/lights/all/state"
headers := { "Authorization": "Bearer " . token , }
data := { "power": "off",  "duration": 0.2 }

MsgBox, % http("POST", url, data, headers)

http(method, url, data := "", headers := "") {
	whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	whr.Open(method, url, true)

	for k, v in headers
		whr.SetRequestHeader(k, v)

	whr.Send(data)
}
Trying to port from python:

Code: Select all

import requests

token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

headers = {
    "Authorization": "Bearer %s" % token,
}

payload = {
    "power": "off",  "duration": 0.2
}

response = requests.put('https://api.lifx.com/v1/lights/all/state', data=payload, headers=headers)
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: WinHttpRequest - A function similar to HttpRequest

14 Oct 2016, 12:10

skribb wrote:Hey guys, trying to control my LIFX but I'm receiving errors (Missing "key" in object literal, and the affected line is the headers := { "Authorization": "Bearer " . token , })
maybe because of the trailing comma , ? why do you have that?

also, why are you using that super simple http() func instead of the full func in this post?

finally, in your python script, you're using the PUT httpmethod but in the ahk script you're using POST. i don't know if that matters or not or if this script even supports it

tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: WinHttpRequest - A function similar to HttpRequest

14 Oct 2016, 21:39

Hi skribb,

Try this http function:

Code: Select all

http(method, url, oData := "", oHeaders := "") {
	static whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	whr.Open(method, url, true)

	for k, v in oHeaders {
		whr.SetRequestHeader(k, v)
	}
	if (method ~= "i)POST|PUT") && !oHeaders["Content-Type"] {
		whr.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
	}

	if IsObject(oData) {
		for k, v in oData {
			sData .= "&" k "=" v
		}
		sData := SubStr(sData, 2)
	}

	whr.Send(sData)
	whr.WaitForResponse()
	return whr.ResponseText
}
And as guest3456 mentioned, you should remove the trailing comma in the headers object and using the PUT method.

Code: Select all

token := "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

url := "https://api.lifx.com/v1/lights/all/state"
headers := { "Authorization": "Bearer " . token }
data := { "power": "off",  "duration": 0.2 }

MsgBox, % http("put", url, data, headers)
garry
Posts: 3764
Joined: 22 Dec 2013, 12:50

Re: WinHttpRequest - A function similar to HttpRequest

16 Oct 2016, 02:31

EDIT = I had problem with XP , works fine with WIN-10
------------------------------------------------------------

I had no success with this (XP)
https://autohotkey.com/download/1.1/version.txt
see last ahk version = 1.1.24.02

Example with ActiveX , can I have this as variable or download to variable with 'WinHttpRequest' ?

Code: Select all

Gui,1:Color, Black
Gui,1:Font,cYellow,FixedSys
;xxa=Shell.Explorer                  ;- IExplorer
xxa=Mozilla.Browser                  ;- firefox
F1:="https://autohotkey.com/download/1.1/version.txt"
Gui Add, ActiveX, x1 y2 w300 h30 vWB,%xxa%
Gui,add,text,x10 y55 ,Your ahk-version is = %a_ahkversion%
WB.Navigate(F1)
Gui, Show, h100
return
GuiClose:
ExitApp
this works with wget.exe ( but is not a variable )
for XP > example with wget compare ahk-version

Code: Select all

setworkingdir,%a_scriptdir%
A =%A_AHKVERSION%

f1x:="https://autohotkey.com/download/1.1/version.txt"

;urldownloadtofile,%f1x%,version.txt                                  ; no success with XP
;return

SplitPath,f1x, name, dir, ext, name_no_ext, drive
ref=%dir%
f2=%a_scriptdir%\%name%

WGET1=%A_SCRIPTDIR%\wget.exe
ifnotexist,%wget1%
  {
  if A_Is64bitOS
    x1=https://eternallybored.org/misc/wget/current/wget64.exe
  else
    x1=https://eternallybored.org/misc/wget/current/wget.exe
  urldownloadtofile,%x1%,wget.exe
  }
ifexist,%f2%
   filedelete,%f2%
runwait,%comspec% /c wget --no-parent --referer=%ref% "%f1x%",,hide
fileread,B,%f2%
   if (A<>B)
     {
     msgbox, 262436,AHK-Version ,(Changed)`nYour existing version is=%A%`n          Actual version is=%B%`n64-bit=%a_is64bitos%`nUnicode=%a_isunicode%`nOS=%a_osversion%`nWant you open Autohotkey downloads page ?
     IfMsgBox,No
       return
     Else
       {
       run,https://autohotkey.com/download/
       ;run,https://autohotkey.com/download/ahk-install.exe
       return
       }
     }
   else
     {
     msgbox, 262436,AHK-Version ,(Equal)`nYour existing version is=%A%`n           Actual version is=%B%`n64-bit=%a_is64bitos%`nUnicode=%a_isunicode%`nOS=%a_osversion%`nWant you open Autohotkey downloads page ?
     IfMsgBox,No
       return
     Else
       run,https://autohotkey.com/download/
    return
    }
return
Last edited by garry on 16 Oct 2016, 15:13, edited 1 time in total.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: WinHttpRequest - A function similar to HttpRequest

16 Oct 2016, 06:11

@garry

Code: Select all

WinHttpRequest("https://autohotkey.com/download/1.1/version.txt", ver)
MsgBox, % ver
Or this:
https://autohotkey.com/docs/commands/URLDownloadToFile.htm#Examples wrote:

Code: Select all

; Example: Download text to a variable:
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", "https://autohotkey.com/download/1.1/version.txt", true)
whr.Send()
; Using 'true' above and the call below allows the script to remain responsive.
whr.WaitForResponse()
version := whr.ResponseText
MsgBox % version
garry
Posts: 3764
Joined: 22 Dec 2013, 12:50

Re: WinHttpRequest - A function similar to HttpRequest

16 Oct 2016, 15:10

@tmplinshi, thank you , works fine with win10 , didn't work with XP

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 163 guests