Jump to content

Get URL Headers?


Recommended Posts

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
Link to comment
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)

Link to comment
Share on other sites

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
Link to comment
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?

Link to comment
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.

Link to comment
Share on other sites

  • 11 months later...

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:

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...