Jump to content

[Solved]How to capture a GET HTPP?


Recommended Posts

Hello,

I am writing a script but I have a problem... And I need your Help! :huh2:

I use winhttp.au3 for download file in the internet

$source = _WinHttpConnect($hOpen,$website)
$file   = _WinHttpSimpleRequest($source,Default,$Link)
msgbox(0,"",$file)

The msgbox it's ok, I see my txt file, but I dont know the name of the txt... I did a capture with wireshark, I show the resultat:

Posted Image

I want know the second or third http request who contains the name of my txt... But I dont know how to do... I tryed to use TCPSend/Recv but it sends only TCP and not HTPP... Then I tryed to use Winpcap.au3 but I cant capture and use _WinHttpSimpleRequest...

There is a solution?

Thank in advance!

Edited by Darako
Link to comment
Share on other sites

If you want to query the headers of the request you can use _WinHttpQueryHeaders, take a look at the example from the help file:

#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

#include "WinHttp.au3"

Opt("MustDeclareVars", 1)

; Open needed handles
Global $hOpen = _WinHttpOpen()
Global $hConnect = _WinHttpConnect($hOpen, "msdn.microsoft.com")
; Specify the reguest:
Global $hRequest = _WinHttpOpenRequest($hConnect, Default, "en-us/library/aa384101(VS.85).aspx")

; Send request
_WinHttpSendRequest($hRequest)

; Wait for the response
_WinHttpReceiveResponse($hRequest)

Global $sHeader
 ; If there is data available...
If _WinHttpQueryDataAvailable($hRequest) Then $sHeader = _WinHttpQueryHeaders($hRequest) ; ...get full header

; Clean
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

; Display retrieved header
MsgBox(0, "Header", $sHeader)

Then you can use regexp to search for the name of the file. You said it's a .txt so you can search for a string that finishes in .txt for instance.

Link to comment
Share on other sites

Hello,

Didn't work :huh2:

When I try the first solution, MsgBox show the header of my first request and I dont see the header of my second or third request...

When I try the second solution:

$file = _WinHttpSimpleRequest($source,Default,$Link,Default,Default,Default,True)
; Return values .: Success      - response data if $fGetHeaders = False (default)
;                  |Array if $fGetHeaders = True
;                  | [0] - response headers
;                  | [1] - response data

But $file is not array... /: and i see response data... ;)

Link to comment
Share on other sites

HTTP is a protocol that uses TCP. If you are already familiar with Wireshark you will find it easy to recreate the behavior of your browser for example. Simply do something like below:

TCPStartup()

$sPacket = "GET / HTTP/1.1" & @CRLF & "Host: yourlink" & @CRLF & @CRLF

$iSocket = TCPConnect("ip here", 80)
TCPSend($iSocket, $sPacket)

; capture output
Link to comment
Share on other sites

Thanks for your post.

I dont understand, didnt work it. I did my TcpSend request with Autoit, my http subsections are identical but my tcpsend request dont receive answer... I can see difference only in TCP subction...

I didnt see why didnt work it... :/

Edited by Darako
Link to comment
Share on other sites

Use Mithrandir's code from post #2 and add this after $hOpen = _WinHttpOpen()

_WinHttpSetOption($hOpen, $WINHTTP_OPTION_REDIRECT_POLICY, $WINHTTP_OPTION_REDIRECT_POLICY_NEVER)

Just turns off server redirects.

You can then extract the Location value from the headers returned in $sHeader

Link to comment
Share on other sites

This is my code i used for this.

Normally you get redirected to the final location of u.to/6GECAQ. By adding that WinHttpSetOption line, i stop redirects then you can grab the location it is going to by extracting Location: from the headers.

#include "WinHttp.au3"


$host = "u.to"
$hGet = "/6GECAQ"



; Initialize and get session handle
Global $hOpen = _WinHttpOpen()


_WinHttpSetOption($hOpen, $WINHTTP_OPTION_REDIRECT_POLICY, $WINHTTP_OPTION_REDIRECT_POLICY_NEVER)


; Get connection handle
Global $hConnect = _WinHttpConnect($hOpen, $host)


; Request
Global $hRequest = _WinHttpOpenRequest($hConnect, "GET", $hGet)


_WinHttpSendRequest($hRequest)

_WinHttpReceiveResponse($hRequest)


Global $sHeader
 ; If there is data available...
If _WinHttpQueryDataAvailable($hRequest) Then $sHeader = _WinHttpQueryHeaders($hRequest) ; ...get full header


; Close handles
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)


MsgBox(0, "Header", $sHeader)
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...