Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

DllCall: HttpQueryInfo - Get HTTP headers


  • Please log in to reply
20 replies to this topic
olfen
  • Members
  • 115 posts
  • Last active: Dec 25 2012 09:48 AM
  • Joined: 04 Jun 2005
Thanks to PhiLho I learned how to parse buffers with binary 0 seperated contents...

/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; HttpQueryInfo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
QueryInfoFlag:

HTTP_QUERY_RAW_HEADERS = 21
Receives all the headers returned by the server.
Each header is terminated by "\0". An additional "\0" terminates the list of headers.

HTTP_QUERY_CONTENT_LENGTH = 5
Retrieves the size of the resource, in bytes.

HTTP_QUERY_CONTENT_TYPE = 1
Receives the content type of the resource (such as text/html).

Find more at: http://msdn.microsoft.com/library/en-us/wininet/wininet/query_info_flags.asp

Proxy Settings:

INTERNET_OPEN_TYPE_PRECONFIG                    0   // use registry configuration
INTERNET_OPEN_TYPE_DIRECT                       1   // direct to net
INTERNET_OPEN_TYPE_PROXY                        3   // via named proxy
INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY  4   // prevent using java/script/INS

*/ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 

HttpQueryInfo(URL, QueryInfoFlag=21, Proxy="", ProxyBypass="") {
hModule := DllCall("LoadLibrary", "str", "wininet.dll") 

If (Proxy != "")
AccessType=3
Else
AccessType=1

io_hInternet := DllCall("wininet\InternetOpenA"
, "str", "" ;lpszAgent
, "uint", AccessType
, "str", Proxy
, "str", ProxyBypass
, "uint", 0) ;dwFlags
If (ErrorLevel != 0 or io_hInternet = 0) {
DllCall("FreeLibrary", "uint", hModule)
return, -1
}

iou_hInternet := DllCall("wininet\InternetOpenUrlA"
, "uint", io_hInternet
, "str", url
, "str", "" ;lpszHeaders
, "uint", 0 ;dwHeadersLength
, "uint", 0x80000000 ;dwFlags: INTERNET_FLAG_RELOAD = 0x80000000 // retrieve the original item
, "uint", 0) ;dwContext
If (ErrorLevel != 0 or iou_hInternet = 0) {
DllCall("FreeLibrary", "uint", hModule)
return, -1
}

VarSetCapacity(buffer, 1024, 0)
VarSetCapacity(buffer_len, 4, 0)

Loop, 5
{
  hqi := DllCall("wininet\HttpQueryInfoA"
  , "uint", iou_hInternet
  , "uint", QueryInfoFlag ;dwInfoLevel
  , "uint", &buffer
  , "uint", &buffer_len
  , "uint", 0) ;lpdwIndex
  If (hqi = 1) {
    hqi=success
    break
  }
}

IfNotEqual, hqi, success, SetEnv, res, timeout

If (hqi = "success") {
p := &buffer
Loop
{
  l := DllCall("lstrlen", "UInt", p)
  VarSetCapacity(tmp_var, l+1, 0)
  DllCall("lstrcpy", "Str", tmp_var, "UInt", p)
  p += l + 1  
  res := res  . "`n" . tmp_var
  If (*p = 0)
  Break
}
StringTrimLeft, res, res, 1
}

DllCall("wininet\InternetCloseHandle",  "uint", iou_hInternet)
DllCall("wininet\InternetCloseHandle",  "uint", io_hInternet)
DllCall("FreeLibrary", "uint", hModule)

return, res
}

Edit: Added a timeout after 5 unsuccessful calls to HttpQueryInfoA.
Edit: Replaced "res := res "`n" . tmp_var" with "res := res . "`n" . tmp_var"
Edit: Made lpszAgent blank

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
I don't have to rely so much on the Firefox Web Developerextension anymore, thanks :)

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
olfen is becoming the WinINET specialist. :-)
Very useful and educational scripts, thanks.
Also thanks for the credit. ^_^'
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

olfen
  • Members
  • 115 posts
  • Last active: Dec 25 2012 09:48 AM
  • Joined: 04 Jun 2005

olfen is becoming the WinINET specialist. :-)

You may call me a beginner when I got GetRTTAndHopCount (vulgo "ping") working :)
What I find very regrettable about these DllCalls is the fact, that they cannot be executed as seperate threads!

Edit: Oops, GetRTTAndHopCount is in Iphlpapi.dll :oops:

Conquer
  • Members
  • 385 posts
  • Last active: Jan 10 2013 02:14 AM
  • Joined: 27 Jun 2006
Is it possible to host connections using this?

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
I still don't understand what you mean by "host connections"...
But then, I am not a network specialist.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

Thalon
  • Members
  • 641 posts
  • Last active: Jan 02 2017 12:17 PM
  • Joined: 12 Jul 2005
Would it be too much to give me a short explanation what I can do with this function?

Thalon

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
It is all in the header:

HTTP_QUERY_RAW_HEADERS = 21
Receives all the headers returned by the server.
Each header is terminated by "\0". An additional "\0" terminates the list of headers.

HTTP_QUERY_CONTENT_LENGTH = 5
Retrieves the size of the resource, in bytes.

HTTP_QUERY_CONTENT_TYPE = 1
Receives the content type of the resource (such as text/html).

For example:
url1 = http://www.autohotkey.com
url2 = http://www.autohotkey.com/download/AutoHotkeyInstall.exe
MsgBox % HttpQueryInfo(url1)
MsgBox % HttpQueryInfo(url2, 5)
MsgBox % HttpQueryInfo(url2, 1)
will return:

HTTP/1.1 200 OK
Date: Mon, 28 Aug 2006 11:04:51 GMT
Server: Apache/2.0.54 (Fedora)
Last-Modified: Sun, 27 Aug 2006 16:52:38 GMT
ETag: "21029b-2ba0-a30ea180"
Accept-Ranges: bytes
Content-Length: 11168
Content-Type: text/html
---------------------------
1749587
---------------------------
application/octet-stream

Extremly useful!
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

Thalon
  • Members
  • 641 posts
  • Last active: Jan 02 2017 12:17 PM
  • Joined: 12 Jul 2005
Yes, I read the header, but it doesn't say me a word :D
With your explanation it is easier to understand for those who hadn't to do with it before...

Would it be possible to add Username/Password-Option for Proxy?

Thx,
Thalon

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Well, looking at InternetOpen function, I see no way to give these. Perhaps it needs another function.
Note that AccessType=0 indicates to use the registry settings (basically IE's settings), so it probably does what you want.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

robiandi
  • Members
  • 49 posts
  • Last active: Jul 27 2008 05:04 PM
  • Joined: 08 Aug 2006

Thalon:

Would it be too much to give me a short explanation what I can do with this function?

PhiLho, thanks to your explanations I got an understanding of the function HttpQueryInfo (although it is all in the header).

The best method is: copy & paste & run (without error)

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

Would it be possible to add Username/Password-Option for Proxy?

The standard HttpQueryInfo("http://user:pass@uri", 21, "proxy", "bypass") should work.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
This is a great script and seems like it would add a lot of value if added to the UrlDownloadToFile page in the documenation. If olfen or anyone else has the time and interest, I could use a volunteer to help adapt this for the documentation:

- Polish it up a little, such as providing more traditional indentation and adding a few more comments (such as why there's a loop that retries the request 5 times).

- Verify what version of MSIE and Windows is needed, and mention it in the comments (maybe it works on all).

- Consider whether there's any way to make it even easier to use: can the parameters be made more friendly; can using a proxy be made easier or explained better; comment on this script's relationship with MSIE's internal settings (if any); and anything else you can think of.

If no one has the time or interest to do this, I'll take care of it at a later time. Thanks to everyone who contributed to this script.

olfen
  • Members
  • 115 posts
  • Last active: Dec 25 2012 09:48 AM
  • Joined: 04 Jun 2005
Thanks for considering adding the script to the documentation.
However, due to lack of time, it might well be 2 or 3 weeks, until I get to overhaul and comment the script.

Kahz
  • Members
  • 190 posts
  • Last active: Feb 14 2009 08:08 PM
  • Joined: 12 Sep 2005
It should be noted that this script returns the header information for the final destination and not necessarily the header of the specified url.

Is it possible to modify this script to properly capture the header from an url with a Location:(url) redirect?