WinHttpRequest - A function similar to HttpRequest

Post your working scripts, libraries and tools
tmplinshi
Posts: 1071
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] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus



: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: 283
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: 1071
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: 283
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: 1071
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: 2
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: 21
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] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus


Trying to port from python:

Code: [Select all] [Expand] [Download] (Untitled.py)GeSHi © Codebox Plus

guest3456
Posts: 2014
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: 1071
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] [Expand] [Download] (http.ahk)GeSHi © Codebox Plus



And as guest3456 mentioned, you should remove the trailing comma in the headers object and using the PUT method.

Code: [Select all] [Download] GeSHi © Codebox Plus

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: 921
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] [Expand] [Download] GeSHi © Codebox Plus



this works with wget.exe ( but is not a variable )
for XP > example with wget compare ahk-version

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Last edited by garry on 16 Oct 2016, 15:13, edited 1 time in total.
tmplinshi
Posts: 1071
Joined: 01 Oct 2013, 14:57

Re: WinHttpRequest - A function similar to HttpRequest

16 Oct 2016, 06:11

@garry

Code: [Select all] [Download] GeSHi © Codebox Plus

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] [Download] GeSHi © Codebox Plus

; 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: 921
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”

Who is online

Users browsing this forum: grant and 21 guests