Sign in to follow this  
Followers 0
AcidicChip

Get URL Headers?

11 posts in this topic

#1 ·  Posted (edited)

Hi all. I'm trying to get the headers from a URL, to determine if it is an html page, audio file, video file, etc via 'File-type' header, and even knowing the file size via 'Content-length' (Even though for that I can use InetGetSize). rather than relying on the path/file extention (if any) of the URL.

Am I able to do this? If so; How?

Thanks in advance.

Edited by AcidicChip

Share this post


Link to post
Share on other sites



How about a way to connect to a port 80, and retreive the headers that way?

Share this post


Link to post
Share on other sites

HttpQueryInfo() function in wininet.dll would help.

InternetOpen() & InternetOpenUrl() must be called before that. You can find an example in C:\Program Files\AutoIt3\include\Inet.au3. Also you may need documentation from MSDN... B)

Share this post


Link to post
Share on other sites

$WinHttpReq = ObjCreate("winhttp.winhttprequest.5.1")
$WinHttpReq.open("GET",$URL)
$WinHttpReq.send()
$header = $WinHttpReq.GetAllResponseHeaders

to get it all

or for ex.

$header = $WinHttpReq.GetResponseHeader("Content-length")

seems easy enough for me B)

Share this post


Link to post
Share on other sites

Thanks for your help! B)

Share this post


Link to post
Share on other sites

Is there a way to do threading? To get the "content-type" it seems to take a bit. Is there a faster way to retreive it? Or maybe do some sort of threading?

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Okay, I thought of a faster way to get the content type, however, when using it repeatedly, it hangs the script, and even appears to hang my computer's internet.

TCPStartup()
Func GetContentType($url)
    $pre = StringMid($url, 1, StringInStr($url, "://")) & "//"
    $temp = StringSplit($url, "/")
    $host = $temp[3]
    $path = StringMid($url, StringLen($pre & $host) + 1)
    

    $sock = TCPConnect(TCPNameToIP($host), 80)
    $req = "GET " & $path & " HTTP/1.0" & @CRLF & "Host:" & $host & @CRLF & @CRLF
    $data = TCPSend($sock, $req)
    
    While 1
        $recv = TCPRecv($sock, 1024)
        If StringLen($recv) Then 
            ExitLoop
        EndIf
    Wend
    
    $temp = StringMid($recv, StringInStr($recv, "content-type: ") + 14)
    $temp = StringMid($temp, 1, StringInStr($temp, @CR) - 1)
    Return $temp
EndFunc

While 1
    MsgBox(0, "", GetContentType("http://www.hiddensoft.com/"))
Wend

Before the loop hits 20 times, it'll freeze... Any idea why?

Edited by AcidicChip

Share this post


Link to post
Share on other sites
B) works fine, no freeze at all

Share this post


Link to post
Share on other sites

B) works fine, no freeze at all

That's interresting. On my computer it freezes on the 17th/18th time and kills my internet connection, and on my wife's computer, I did it up to 50 times (Got bored hitting 'OK') with no problems. I wonder why that is.

I'm going to load up a Virtual Machine and try it on there, and install software on it, to see what the problem is. Anyone else having this problem and/or know why?

Share this post


Link to post
Share on other sites

On my virtual machine, I have installed the same exact windows, anti-virus, updates, autoit, and web browsers. So, it's none of them. I'll test it on my computer at work tomorrow, I have the same software installed on that computer, as I do on mine. If it doesn't freeze there, then it's mostlikely some network setting on my computer or router.

Share this post


Link to post
Share on other sites

I hope nobody minds me re-opening an almost year old-dead thread. :lmao:

I was trying to get HTTP header information (last-modified date) with as little overhead as possible when I stumbled upon this thread.

$WinHttpReq = ObjCreate("winhttp.winhttprequest.5.1")
$WinHttpReq.open("GET",$URL)
$WinHttpReq.send()
$header = $WinHttpReq.GetAllResponseHeaders

to get it all`

or for ex.

$header = $WinHttpReq.GetResponseHeader("Content-length")

seems easy enough for me ;)

Both @Luffy and @AcidicChip noted that sometimes the system would seem to hang.

The problem is the HTTP Request Verb being used is GET. You are asking the server to send you the entire contents of the url. :evil:

Solution: just change the request verb from GET to HEAD (just send the header information)

$WinHttpReq = ObjCreate("winhttp.winhttprequest.5.1")
$WinHttpReq.open("HEAD",$URL)
$WinHttpReq.send()
$header = $WinHttpReq.GetAllResponseHeaders  ;should always work

Using GetAllResponseHeaders should always work (providing the host can be contacted).

Attempting to get a specific piece of information that is not available will fail.

Bill :mad:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0