quarqo

WinHTTP get redirected location

17 posts in this topic

Hello autoit community!

I am struggling again with a (probably noobish) issue and I am seeking for you help.

Basically here is what I am trying to do:

I am using the WinHTTP.au3 UDF to make some requests to a website that redirect to another webpage. Here come the question, how can I get the url of the website that the request lands after redirection?

I am using a simple code:

; Initialize and get session handle
Global $hOpen = _WinHttpOpen()
; Get connection handle
Global $hConnect = _WinHttpConnect($hOpen, $initialurl)
; Make a request
Global $hRequest = _WinHttpSimpleSendRequest($hConnect, Default, "/") ;Here the request follow the redirection and land on a different webpage
; Close handles
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

I am really looking forward to see if there is any solution.

Quarqo.

Share this post


Link to post
Share on other sites



Hmm.. Maybe:

_WinHttpQueryHeaders()

?


Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

You can do it like this:

#include "WinHttp.au3"


$initialurl = "http://google.com"

; Initialize and get session handle
$hOpen = _WinHttpOpen()
; Get connection handle
$hConnect = _WinHttpConnect($hOpen, $initialurl)

; Register Callback function
$hREDIRECT_CALLBACK = DllCallbackRegister(__Redirect, "none", "handle;dword_ptr;dword;ptr;dword")
; Set callback
_WinHttpSetStatusCallback($hConnect, $hREDIRECT_CALLBACK, $WINHTTP_CALLBACK_FLAG_REDIRECT)

; Make a request
$hRequest = _WinHttpSimpleSendRequest($hConnect, Default, "/") ;Here the request follow the redirection and land on a different webpage

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

; Free callback
DllCallbackFree($hREDIRECT_CALLBACK)




; Define callback function
Func __Redirect($hInternet, $iContext, $iInternetStatus, $pStatusInformation, $iStatusInformationLength)
        Local $sStatus = "About to automatically redirect the request to: " & DllStructGetData(DllStructCreate("wchar[" & $iStatusInformationLength & "]", $pStatusInformation), 1) & "    "
        ConsoleWrite("!>" & $sStatus & @CRLF)
        MsgBox(4096, "REDIRECTION:", $sStatus)
EndFunc

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Hmm.. Maybe:

_WinHttpQueryHeaders()

?

Thank you for the reply. I've already tried that. It returns all the response headers from the redirected website but nothing about the location url.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

 

You can do it like this:

#include "WinHttp.au3"


$initialurl = "http://google.com"

; Initialize and get session handle
$hOpen = _WinHttpOpen()
; Get connection handle
$hConnect = _WinHttpConnect($hOpen, $initialurl)

; Register Callback function
$hREDIRECT_CALLBACK = DllCallbackRegister(__Redirect, "none", "handle;dword_ptr;dword;ptr;dword")
; Set callback
_WinHttpSetStatusCallback($hConnect, $hREDIRECT_CALLBACK, $WINHTTP_CALLBACK_FLAG_REDIRECT)

; Make a request
$hRequest = _WinHttpSimpleSendRequest($hConnect, Default, "/") ;Here the request follow the redirection and land on a different webpage

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

; Free callback
DllCallbackFree($hREDIRECT_CALLBACK)




; Define callback function
Func __Redirect($hInternet, $iContext, $iInternetStatus, $pStatusInformation, $iStatusInformationLength)
        Local $sStatus = "About to automatically redirect the request to: " & DllStructGetData(DllStructCreate("wchar[" & $iStatusInformationLength & "]", $pStatusInformation), 1) & "    "
        ConsoleWrite("!>" & $sStatus & @CRLF)
        MsgBox(4096, "REDIRECTION:", $sStatus)
EndFunc

Thank you sooo much, Trancexx! This is indeed returning the url I want but can you help me to save that $sTatus url into a variable for further use? xD

EDIT

Got it working. Just declared a variable and added it as parameter in the _Redirect function, into which I've attached the $sStatus url over it.

Thank you so much again! *biiig hug*

Edited by quarqo

Share this post


Link to post
Share on other sites

Hi, I'm kinda new to this and this is the only post I could find on this subject but I am not able to get this code to work. The _WinHTTPSimpleSendRequest method is setting @error to 1. Has there been a change in the au3 since Sept? Possibly something in my environment? Please let me know if you have any idea why this isn't working. Thanks!

Share this post


Link to post
Share on other sites

jchd will not be please by this dig up.

Can you provide your code? I'm using winhttp v1.6.3.6, and to disable redirects so far I'm doing like this:

_WinHttpSetOption($hRequest, $WINHTTP_OPTION_DISABLE_FEATURE, $WINHTTP_OPTION_REDIRECT_POLICY_NEVER)
...
...
_WinHttpSendRequest($hRequest)
_WinHttpReceiveResponse($hRequest)
If _WinHttpQueryDataAvailable($hRequest) Then
    $sLocation = _WinHttpQueryHeaders($hRequest, 33) ;33=location:
Endif

 


Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

Thank you, and I apologize if I did something that is contrary to the forum guidelines or rules. I am using WinHTTP v. 1.6.3.8 and the code that is posted by trancexx is what I have been testing. I haven't altered it other than to put quotes around the function name in the DllCallbackRegister method call.

Share this post


Link to post
Share on other sites

with 1.6.3.6 works

output

---------------------------
REDIRECTION:
---------------------------
About to automatically redirect the request to: http://www.google.pt/?gfe_rd=cr&ei=JSZRVe2qB_Kr8we6goCQAg    
---------------------------
OK   
---------------------------

 


Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

Thank you, and I apologize if I did something that is contrary to the forum guidelines or rules. I am using WinHTTP v. 1.6.3.8 and the code that is posted by trancexx is what I have been testing. I haven't altered it other than to put quotes around the function name in the DllCallbackRegister method call.

​I don't see what change could have caused the code not to work for you.
Could you really post the exact code you run, no matter how dumb this request might sound to you? But do run it before posting it. The code posted here should run fine.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

​I don't see what change could have caused the code not to work for you.Could you really post the exact code you run, no matter how dumb this request might sound to you? But do run it before posting it. The code posted here should run fine.

#include "WinHttp.au3"


$initialurl = "http://google.com"

; Initialize and get session handle
$hOpen = _WinHttpOpen()
; Get connection handle
$hConnect = _WinHttpConnect($hOpen, $initialurl)

; Register Callback function
$hREDIRECT_CALLBACK = DllCallbackRegister("_Redirect", "none", "handle;dword_ptr;dword;ptr;dword")
; Set callback
_WinHttpSetStatusCallback($hConnect, $hREDIRECT_CALLBACK, $WINHTTP_CALLBACK_FLAG_REDIRECT)

; Make a request
$hRequest = _WinHttpSimpleSendRequest($hConnect, Default, "/") ;Here the request follow the redirection and land on a different webpage
MsgBox(1, "Testing", @error)
; Close handles
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

; Free callback
DllCallbackFree($hREDIRECT_CALLBACK)




; Define callback function
Func _Redirect($hConnect, $iContext, $iInternetStatus, $pStatusInformation, $iStatusInformationLength)
        Local $sStatus = "About to automatically redirect the request to: " & DllStructGetData(DllStructCreate("wchar[" & $iStatusInformationLength & "]", $pStatusInformation), 1) & "    "
        ConsoleWrite("!>" & $sStatus & @CRLF)
        MsgBox(4096, "REDIRECTION:", $sStatus)
EndFunc

This is the code I am testing. I can get the msgbox to tell me the @error is 1 but that is the only msgbox that shows up.

test.au3

Share this post


Link to post
Share on other sites

You need to verify that you run version of WinHttp.au3 greater than v1.6.3.3.
It doesn't matter what SciTE opens. You open manually WinHttp.au3 from script's dir and see what version it is. If indeed it's latest like you say then download it again.

3 sometimes looks like 8. :)


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

You need to verify that you run version of WinHttp.au3 greater than v1.6.3.3.
It doesn't matter what SciTE opens. You open manually WinHttp.au3 from script's dir and see what version it is. If indeed it's latest like you say then download it again.

3 sometimes looks like 8. :)

​I downloaded 1.6.3.6 and put that in the same folder as the test. The help file that came with the code says,"WinHttp-UDFs for AutoIt and this helpfile are created by trancexx and ProgAndy. Current version is 1.6.3.6"

Share this post


Link to post
Share on other sites

Ok. Download attachment from this post, extract "Test" folder and then run Redirect.au3 script. Tell me if that works for you. Test.zip


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Ok. Download attachment from this post, extract "Test" folder and then run Redirect.au3 script. Tell me if that works for you. Test.zip

​This is what I get when I run the script "Redirect.au3"

Redirect error.JPG

Share this post


Link to post
Share on other sites

That's great, thanks for testing.

So, WinHttp.au3 requires AutoIt v3.3.7.20 or above. You run some older version of AutoIt. I can't help you with that AutoIt.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

That's great, thanks for testing.

So, WinHttp.au3 requires AutoIt v3.3.7.20 or above. You run some older version of AutoIt. I can't help you with that AutoIt.

​Oh, ok. I grabbed this one off the AutoIt website but it may have been a little while ago. I'll update, try it again and get back to the thread if I have any more questions. Thank you so much for looking into this. If I could, I would by you a pizza.

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