Sign in to follow this  
Followers 0
UNO

Getting Response status code of any site

8 posts in this topic

I need to get the response status code when I enter a URL name.

For example www.yahoo.com or any secure site i.e https

Following is my script(almost the same as the example)

#include <HTTP.au3>

$host = "www.yahoo.com"

$page = "/overload/autoit/getpage.php"

;ConsoleWrite("Example GET Request:"&@CRLF)

$socket = _HTTPConnect($host)

;ConsoleWrite("Socket Created: "&$socket&@CRLF)

$get = _HTTPGet($host, $page, $socket)

;ConsoleWrite("Bytes sent: "&$get&@CRLF)

$recv = _HTTPRead($socket,1)

If @error Then

;ConsoleWrite("_HTTPRead Error: "&@error&@CRLF)

;ConsoleWrite("_HTTPRead Return Value: "&$recv &@CRLF)

Msgbox(0,"HTTP Return Code: ",$recv&@CRLF)

Else

Msgbox(0,"HTTP Return Code: ",$recv[0]&@CRLF)

;ConsoleWrite("HTTP Return Code: "&$recv[0]&@CRLF)

;ConsoleWrite("HTTP Return Response: "&$recv[1]&@CRLF)

;ConsoleWrite("Number of headers: "&UBound($recv[3])&@CRLF)

;ConsoleWrite("Size of data downloaded: "&StringLen($recv[4])&" bytes"&@CRLF)

;ConsoleWrite("Page downloaded: "&@CRLF&$recv[4]&@CRLF)

EndIf

_HTTPClose($socket)

Also Consolewrite doesnt work in may case.so i am using Msgbox

Any guesses and help?Or am i using it wrong?

Thanks in advance

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Ehm... HTML udf? :whistle: lol

Global Const $Packet = "GET / HTTP/1.0" & @CRLF & @CRLF
;Global Const $Packet = "GET /overload/autoit/getpage.php HTTP/1.0" & @CRLF & @CRLF

TCPStartup()

Dim $URL = "www.yahoo.com"
Dim $IP = TCPNameToIP($URL)
ConsoleWrite($URL & " -> " & $IP & @CRLF)

Dim $Socket = TCPConnect($IP,80)

ConsoleWrite("Connection return: " & $Socket & " (-1 = error)" & @CRLF)

TCPSend($Socket,$Packet)

ConsoleWrite("Packet send: " & $Packet & @CRLF )

Dim $Data = ''
While 1
    $Data &= TCPRecv($Socket,1024)
    If @error Then ExitLoop
    ConsoleWrite($Data)
WEnd

ConsoleWrite(@CRLF & "Connection terminated." & @CRLF)

$Split = StringSplit($Data,@LF,1)
$ConnectionStatus = $Split[1]

MsgBox(0, "Connection status", $ConnectionStatus )

Edit: Kudos for the TCP functions.. :]

Edited by Manadar

Share this post


Link to post
Share on other sites

Hi Manadar,

Thanks for helping me but it didnt work out.See the image below.

Also how do I revamp it up for for https:// pages

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Hi Manadar,

Thanks for helping me but it didnt work out.See the image below.

Also how do I revamp it up for for https:// pages

I have no clue how https works.. I'm just an hobbyist.

http://en.wikipedia.org/wiki/Https#How_it_works

By the way, I've copied and paste the code that I put up on these forums, and without changing anything it works like expected and gives me 200 OK message. I do not see a reason why AutoIt would behave differently on your side..

Edit: This may work for you...

CODE
#include <Array.au3>

Global Const $Packet = "GET / HTTP/1.0" & @CRLF & @CRLF
;Global Const $Packet = "GET /overload/autoit/getpage.php HTTP/1.0" & @CRLF & @CRLF

TCPStartup()

Dim $URL = "www.yahoo.com"
Dim $IP = TCPNameToIP($URL)
ConsoleWrite($URL & " -> " & $IP & @CRLF)

Dim $Socket = TCPConnect($IP,80)

ConsoleWrite("Connection return: " & $Socket & " (-1 = error)" & @CRLF)

TCPSend($Socket,$Packet)

ConsoleWrite("Packet send: " & $Packet & @CRLF )

Dim $Data = ''
While 1
    $Data &= TCPRecv($Socket,1024)
    If @error Then ExitLoop
    ConsoleWrite($Data)
WEnd

ConsoleWrite(@CRLF & "Connection terminated." & @CRLF)

$Split = StringSplit($Data,@LF,1)
_ArrayDisplay($Split,"")
Edited by Manadar

Share this post


Link to post
Share on other sites

still not working.Maybe i will replace consolewrite with msgbox.

and the status code is displayed in arraydisplay?

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

lol, what version of AutoIt are you using? Is that release or beta?

Yes the status code, has to be somewhere in the MsgBox.

Edited by Manadar

Share this post


Link to post
Share on other sites

I am using the release.

The problem isnt resolved...

:shocked:

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