vb.netasp-classichttp-status-code-301http-redirectserverxmlhttp

ServerXMLHTTP request returing data but not returning url of final page after 301 redirection


I am trying to make LINK FINDER app in ASP
It working is divided into 5 step

  1. Send http request to server at www.foo.com
  2. Check status of request
  3. If its 200 then move to step 4 otherwise show error
  4. Parse all link
  5. Send http request to server to parsed link

I am able to do first 4 step, But facing challenge in 5th step

I am getting 3 type of links

1.)absolute link : http://www.foo.com/file.asp
2.)links from root directory, which need domain name eg /folder2/file2.asp
3.)relative link : ../file3.asp

Challenge

When I am requesting www.foo.com , which is 301 redirected to www.foo.com/folder3/folder3/file3.asp

I am getting html content of redirected page, But don't get redirected url and not able to check 3rd type of links

Using following code

Set ServerXmlHttp = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
ServerXmlHttp.open "GET", "http://www.foo.com"
ServerXmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
ServerXmlHttp.send PostData
If ServerXmlHttp.status = 200 Then
 //My CODE

Hope for quick response... or any other idea for link finder in asp , vb.net


Solution

  • It's out of the ServerXMLHTTP capabilities.
    Instead you have to use IWinHttpRequest or another third-party component that able to manage redirects.
    In the following example, req.Option(WHR_URL) returns the current url even if redirected.
    Option req.option(WHR_EnableRedirects) is True by default like ServerXMLHTTP.
    So, I've added a line commented out showing how to disable redirects.

    Const WHR_URL = 1
    Const WHR_EnableRedirects = 6
    'Enum constants are listed at http://msdn.microsoft.com/en-us/library/windows/desktop/aa384108(v=vs.85).aspx
    Dim req
    Set req = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
        'req.Option(WHR_EnableRedirects) = False 'don't follow the redirects
        req.open "GET", "http://www.foo.com", False
        req.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        req.send PostData
    If req.Status = 200 Then
        Response.Write "Last URL : " & req.Option(WHR_URL)
    End If