Jump to content

WinHTTP get redirected location


quarqo
 Share

Go to solution Solved by trancexx,

Recommended Posts

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.

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

 

Link to comment
Share on other sites

  • Solution

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

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

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

  • 7 months later...

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!

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

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

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

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

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

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

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

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

Link to comment
Share on other sites

  • 7 years later...

I have read the above posts because i have a similar problem. I have a url which is redirected and want to get the target url. The target changes often, so i wanted to detect it with some code.

Here the url i tried : https://go.microsoft.com/fwlink/?LinkID=2093505

...but the above scripts didn't give me anything back... Any idea why ?

My AutoIt is actual - also the winhttp.au3

Edited by agivx3
Link to comment
Share on other sites

@agivx3

If your primary goal is to capture the new location of a redirect response, then the example below should help.  If your primary goal is to figure out why your script didn't work using the WinHttp UDF, then maybe someone else will come along and help you with that.  Of course that assumes that this thread isn't closed first, since you revived it after being dormant for 7 years. :rolleyes:

Example:

#include <Constants.au3>


http_redirect_example("https://go.microsoft.com/fwlink/?LinkID=2093505")

Func http_redirect_example($sURL)
    Const $OPTION_ENABLE_REDIRECTS = 6

    ;Register COM Error Handler
    Local $oComErr = ObjEvent("AutoIt.Error", com_error_handler)
    If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "Unable to register COM error handler - @error = " & @error)

    With ObjCreate("winhttp.winhttprequest.5.1")
        ;Open GET request
        .Open("GET", $sURL)
        If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", StringFormat("(0x%X) %s", $oComErr.RetCode, $oComErr.WinDescription))

        ;Set option to disable redirects in order to capture 301 and 302 responses
        .Option($OPTION_ENABLE_REDIRECTS) = False

        ;Send the request
        .Send()
        If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", StringFormat("(0x%X) %s", $oComErr.RetCode, $oComErr.Description))

        ConsoleWrite("Requested URL: " & $sURL & @CRLF)
        ConsoleWrite(StringFormat("HTTP Status:   %s %s", .Status, .StatusText) & @CRLF)

        ;If http status code is 301 or 302 (Redirect)
        If .Status = 301 Or .Status = 302 Then
            ;Display new location
            ConsoleWrite("New Location:  " & .GetResponseHeader("Location") & @CRLF)
            Return
        EndIf

        ;Display response
        ConsoleWrite(@CRLF & "HTTP Response:" & @CRLF)
        ConsoleWrite(.ResponseText    & @CRLF)
    EndWith
EndFunc

Func com_error_handler($oError)
    With $oError
        ConsoleWrite(@CRLF)
        ConsoleWrite("COM ERROR:" & @CRLF)
        ConsoleWrite("- Error Number........... " & "0x" & Hex(.number) & @CRLF)
        ConsoleWrite("- Error WinDescription... " & StringStripWS(.windescription, $STR_STRIPTRAILING) & @CRLF)
        ConsoleWrite("- Error Description...... " & StringStripWS(.description   , $STR_STRIPTRAILING) & @CRLF)
        ConsoleWrite("- Error ScriptLine....... " & .scriptline & @CRLF)
        ConsoleWrite("- Error RetCode.......... " & "0x" & Hex(.retcode) & @CRLF)
    EndWith

    Return ; Return so @error can be trapped by the calling function
EndFunc

Example's output:

Requested URL: https://go.microsoft.com/fwlink/?LinkID=2093505
HTTP Status:   302 Moved Temporarily
New Location:  https://msedge.sf.dl.delivery.mp.microsoft.com/filestreamingservice/files/f343fe88-b98f-4578-9e6b-0534cc919974/MicrosoftEdgeEnterpriseX86.msi

 

Edited by TheXman
Link to comment
Share on other sites

Thank you very much, that was exact what i was looking for. It's working perfect for the example string.

But i just tried another redirected url  which seems to give no new location back ?

https://chromeenterprise.google/browser/download/?platform=WIN_MSI&channel=stable&usagestats=0

Could you please take a look at this one too?

The redirected url should be this one:

https://dl.google.com/tag/s/appguid%3D%7B8A69D345-D564-463C-AFF1-A69D9E530F96%7D%26iid%3D%7B61FD9C8E-3593-993D-C216-4DAC2E21AFA1%7D%26lang%3Den%26browser%3D4%26usagestats%3D0%26appname%3DGoogle%2520Chrome%26needsadmin%3Dtrue%26ap%3Dstable-arch_x86-statsdef_0%26brand%3DGCEA/dl/chrome/install/googlechromestandaloneenterprise.msi

 

Edited by agivx3
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...