Page 1 of 1

WinHttpRequest - A function similar to HttpRequest

Posted: 03 Apr 2014, 02:05
by tmplinshi
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.

Re: WinHttpRequest - A function similar to HttpRequest

Posted: 03 Apr 2014, 04:30
by Guest10
successfully tested! :ugeek:

Re: WinHttpRequest - A function similar to HttpRequest

Posted: 04 Apr 2014, 02:22
by ozzii
Just a thought:
You can add an test for the existence of the winhttp.dll file

Re: WinHttpRequest - A function similar to HttpRequest

Posted: 04 Apr 2014, 02:45
by tmplinshi
@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).

Re: WinHttpRequest - A function similar to HttpRequest

Posted: 06 Apr 2014, 08:40
by Don_Corleon
nice work.
I will have to test it out and see if it can help to solve my ongoing http issues :)

Re: WinHttpRequest - A function similar to HttpRequest

Posted: 07 Apr 2014, 11:44
by ozzii
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.

Re: WinHttpRequest - A function similar to HttpRequest

Posted: 07 Apr 2014, 12:00
by tmplinshi
@ozzii
Ok, got it. Thanks :)

Re: WinHttpRequest - A function similar to HttpRequest

Posted: 01 Jul 2016, 06:12
by Dave_Scream
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\

Re: WinHttpRequest - A function similar to HttpRequest

Posted: 14 Oct 2016, 11:17
by skribb
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)

Re: WinHttpRequest - A function similar to HttpRequest

Posted: 14 Oct 2016, 12:10
by guest3456
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

Re: WinHttpRequest - A function similar to HttpRequest

Posted: 14 Oct 2016, 21:39
by tmplinshi
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)

Re: WinHttpRequest - A function similar to HttpRequest

Posted: 16 Oct 2016, 02:31
by garry
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

Re: WinHttpRequest - A function similar to HttpRequest

Posted: 16 Oct 2016, 06:11
by tmplinshi
@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

Re: WinHttpRequest - A function similar to HttpRequest

Posted: 16 Oct 2016, 15:10
by garry
@tmplinshi, thank you , works fine with win10 , didn't work with XP