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

WinHttpRequest

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

WinHTTP Redirect

 Keywords: WinHTTP Redirect WinHttp.WinHttpRequest.5.1 SetCredentials

Question:

A web site we routinely work with recently changed their code and now my script is not working. I'm using the ObjectOpen for WinHttp. Here is my code snippet:
WinHttpReq = ObjectOpen("WinHttp.WinHttpRequest.5.1")
a=WinHttpReq.Open("GET", url, @FALSE)
b=WinHttpReq.SetCredentials(uid, pwd, 0)
WinHttpReq.Option(4) = 13056
c=WinHttpReq.Send()
d=WinHttpReq.WaitForResponse()
e=WinHttpReq.ResponseText
This would get the text of the page that I could then parse through and look for the data I needed. It has worked for years.

Now, it appeas that they are doing some kind of redirect when I come to this page, and the path is no longer static. They insert some string of characters.

When I turn on a Http Header trace in my browser and do the steps manually to login and access the page, I'm getting a status code of 302 and the location is then the new page it displays in my browser.

HTTP/1.1 302 Found
Date: Wed, 04 May 2011 21:24:19 GMT
Server: Microsoft-IIS/6.0
MicrosoftOfficeWebServer: 5.0_Pub
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Location: /appid/(u53bg045lmce5j45410yahzp)/View.aspx
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 164
I tried adding in the following to my options for the WinHttpRequestOption_EnableRedirects:
WinHttpReq.Option(6) = @True
Does anyone know how I can handle this or how I can troubleshoot what is coming back in those commands? It has been a while since I got this script working, so there are many cobwebs I'm trying to dust off.

Before getting the 302 response, my Http Header trace on my IE browser is first showing a 401 (Unauthorized) response, then it gets the 302 (redirect) response with the new location. And that location changes any time I open a new session to this web site.

Answer:

WinHttp is supposed to handle redirects. However you can tell the code to handle them. The code might look something like this:
pwd = ""
uid = ""
EnableRedirects = 6

oWinHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
oWinHTTP.SetCredentials(uid, pwd, 0)
oWinHTTP.Option(EnableRedirects) = @FALSE

oWinHTTP.Open("GET", url, @FALSE)
oWinHTTP.Send()
d=oWinHTTP.WaitForResponse()

If oWinHTTP.Status != 200
   If oWinHTTP.Status == 302
      Pause( "Server Attempted Redirect to: ", oWinHTTP.getResponseHeader("Location"))
   EndIf
   Status = oWinHTTP.Status
   StatusText = oWinHTTP.StatusText
   headers = oWinHTTP.GetAllResponseHeaders()
   Pause(oWinHTTP.Status, headers)
EndIf
e=oWinHTTP.ResponseText
Pause('', e)

oWinHTTP = 0
Exit

User Reply:

Thanks for the example. I figured out yesterday that I had to set that Enable Redirects to False and then get the response header. When I tried to pass the location and do another GET, it didn't work.

The key was that I needed to do my SetCredentials statement again between my second Open and Send statements with the new location. Then it worked fine.

Thank you very much for your time and sample script.


Article ID:   W18214
Filename:   WinHTTP Redirect .txt
File Created: 2011:05:06:07:57:02
Last Updated: 2011:05:06:07:57:02