AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

DllCall: HttpQueryInfo - Get HTTP headers
Goto page 1, 2  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
olfen



Joined: 04 Jun 2005
Posts: 113
Location: Stuttgart, Germany

PostPosted: Thu Jun 15, 2006 7:07 pm    Post subject: DllCall: HttpQueryInfo - Get HTTP headers Reply with quote

Thanks to PhiLho I learned how to parse buffers with binary 0 seperated contents...

Code:

/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 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


Last edited by olfen on Fri Jun 16, 2006 6:15 pm; edited 4 times in total
Back to top
View user's profile Send private message Visit poster's website
polyethene



Joined: 11 Aug 2004
Posts: 5244
Location: UK

PostPosted: Thu Jun 15, 2006 7:36 pm    Post subject: Reply with quote

I don't have to rely so much on the Firefox Web Developer extension anymore, thanks Smile
_________________
GitHubScriptsIronAHK Contact by email not private message.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
PhiLho



Joined: 27 Dec 2005
Posts: 6836
Location: France (near Paris)

PostPosted: Fri Jun 16, 2006 9:46 am    Post subject: Reply with quote

olfen is becoming the WinINET specialist. Smile
Very useful and educational scripts, thanks.
Also thanks for the credit. ^_^'
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
olfen



Joined: 04 Jun 2005
Posts: 113
Location: Stuttgart, Germany

PostPosted: Fri Jun 16, 2006 6:14 pm    Post subject: Reply with quote

PhiLho wrote:
olfen is becoming the WinINET specialist. Smile

You may call me a beginner when I got GetRTTAndHopCount (vulgo "ping") working Smile
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 Embarassed
Back to top
View user's profile Send private message Visit poster's website
Conquer



Joined: 27 Jun 2006
Posts: 385
Location: Canada

PostPosted: Sat Aug 26, 2006 2:30 pm    Post subject: Reply with quote

Is it possible to host connections using this?
Back to top
View user's profile Send private message
PhiLho



Joined: 27 Dec 2005
Posts: 6836
Location: France (near Paris)

PostPosted: Mon Aug 28, 2006 8:24 am    Post subject: Reply with quote

I still don't understand what you mean by "host connections"...
But then, I am not a network specialist.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
Thalon



Joined: 12 Jul 2005
Posts: 633

PostPosted: Mon Aug 28, 2006 10:58 am    Post subject: Reply with quote

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

Thalon
_________________
AHK-Icon-Changer
AHK-IRC
deutsches Forum
Back to top
View user's profile Send private message
PhiLho



Joined: 27 Dec 2005
Posts: 6836
Location: France (near Paris)

PostPosted: Mon Aug 28, 2006 11:08 am    Post subject: Reply with quote

It is all in the header:
Quote:
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:
Code:
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:
Quote:
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!
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
Thalon



Joined: 12 Jul 2005
Posts: 633

PostPosted: Mon Aug 28, 2006 11:25 am    Post subject: Reply with quote

Yes, I read the header, but it doesn't say me a word Very Happy
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
_________________
AHK-Icon-Changer
AHK-IRC
deutsches Forum
Back to top
View user's profile Send private message
PhiLho



Joined: 27 Dec 2005
Posts: 6836
Location: France (near Paris)

PostPosted: Mon Aug 28, 2006 11:52 am    Post subject: Reply with quote

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.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
robiandi



Joined: 08 Aug 2006
Posts: 49

PostPosted: Mon Aug 28, 2006 7:02 pm    Post subject: Reply with quote

Quote:
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)
Back to top
View user's profile Send private message
polyethene



Joined: 11 Aug 2004
Posts: 5244
Location: UK

PostPosted: Mon Aug 28, 2006 8:52 pm    Post subject: Reply with quote

Thalon wrote:
Would it be possible to add Username/Password-Option for Proxy?
The standard HttpQueryInfo("http://user:pass@uri", 21, "proxy", "bypass") should work.
_________________
GitHubScriptsIronAHK Contact by email not private message.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10716

PostPosted: Wed Sep 27, 2006 5:14 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Send e-mail
olfen



Joined: 04 Jun 2005
Posts: 113
Location: Stuttgart, Germany

PostPosted: Sat Sep 30, 2006 7:49 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Visit poster's website
Kahz



Joined: 12 Sep 2005
Posts: 190

PostPosted: Fri Nov 17, 2006 1:01 am    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group