WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

WinInet
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Handle Http Redirect

 KEYWORDS: 302 redirect HTTP_STATUS_REDIRECT

Question:

When using the WinInet extender, if you GET a page from an http server and it gives you back a 302 redirect to another page, the extender will dutifully go and fetch the replacement page. All fine and good.

Question: is there any way to determine the URL of the page you have been sent to? These frequently contain useful info like session ids.

Thanks

Answer:

No built in functionality to handle that using the WInInet Extender.By default the WinInet flag "INTERNET_FLAG_NO_AUTO_REDIRECT" is notspecified, this means you have no chance to get the real URL, because Wininet handles the redirection internally and will not pass the HTTP headers of the initial (redirected) request to your script.

Possible but difficult workaround: You could attempt to make a DllCall to HttpOpenRequest and set the flag "INTERNET_FLAG_NO_AUTO_REDIRECT". This tells Wininet to not perform any automatically handling of redirected URLs. You would have to implement it for yourself with the benefit of getting the "real" URL -- look for the "Location" HTTP header field in the first HTTP response. If you do set the flag "INTERNET_FLAG_NO_AUTO_REDIRECT" you have to check the HTTP status code for:

HTTP_STATUS_MOVED            /* 301 -- object permanently moved */ 
HTTP_STATUS_REDIRECT         /* 302 -- object temporarily moved */ 
HTTP_STATUS_REDIRECT_METHOD  /* 303 -- redirection w/ new access method */ 
If one of those HTTP status codes apply, you have to look for the HTTP header field "Location" (iHttpHeaders) to get the "real" URL. To get that "real" URL, you have to perform a new HTTP request with that URL.

Alternative:
You might be able to obtain that information using Internet Explorers object model. but this would require you to rewrite your code.

User Reply:

Yup, a call like this works as a replacement for iHttpInit:
INTERNET_FLAG_NO_AUTO_REDIRECT = 2097152
datahandle = DllCall("WinInet.dll", long:"HttpOpenRequestA", long:connecthandle, lpstr:"GET", lpstr:geturl, lpstr:"HTTP/1.1", lpstr:"", long:0, long:INTERNET_FLAG_NO_AUTO_REDIRECT, long:0)
If you're using any of the flags supported by iHttpInit you'll need to look up the appropriate values and OR them in with INTERNET_FLAG_NO_AUTO_REDIRECT.

If you're trying to do this for an https resource you need to make two changes:

(1) change the flag to INTERNET_FLAG_NO_AUTO_REDIRECT | INTERNET_FLAG_SECURE

where the latter value is 8388608, and

(2) strip the first character from connecthandle - WinBatch prepends the handle with an S, presumably to let iHttpInit know that it needs to set INTERNET_FLAG_SECURE.

Here is a UDF I created that can be used to replace the iHttpInit statement:

#DefineFunction iHttpInitNoRedir (connecthandle, verb, object, referrer, flags)
   INTERNET_FLAG_NO_AUTO_REDIRECT = 2097152
   INTERNET_FLAG_SECURE = 8388608

   flags = flags | INTERNET_FLAG_NO_AUTO_REDIRECT
   If StrSub(connecthandle, 1, 1) == "S"
      connecthandle = StrSub(connecthandle, 2, -1)
      flags = flags | INTERNET_FLAG_SECURE
   EndIf
   Return DllCall("WinInet.dll", long:"HttpOpenRequestA", long:connecthandle, lpstr:verb, lpstr:object, lpstr:"HTTP/1.1", lpstr:referrer, long:0, long:flags, long:0)
#EndFunction

Article ID:   W16886
File Created: 2007:07:03:14:26:42
Last Updated: 2007:07:03:14:26:42