Search code examples
asp-classicmsxmlserverxmlhttp

MSXML2.ServerXMLHTTP using Classic ASP returns broken images


I haven't used MSXML2.ServerXMLHTTP in years and now i need to. When i use MSXML2.ServerXMLHTTP to grab a page, the page returns with broken images. I remember doing this in the past, there was a line of code i would use and the images would resolve perfectly. It was sort of like setting the base url. Does anyone know what the code would be? Here's the code i'm using:

url = "notimportant.com"

Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP")
    objXML.Open "GET", URL, False
    objXML.Send()
    xmlResponse = objXML.responseText
Set objXML = Nothing

Solution

  • You probably want to place a <base> tag inside the <head> so that one line of code must be the following:

    xmlResponse = Replace(objXML.responseText, "<head>", "<head><base href=""http://notimportant.com/"" />", 1, 1, vbTextCompare)
    

    Or as a more reliable way in case where the head tag is more complex and unpredictable like <head class="head etc">, you can use regular expressions to replace:

    Dim Re
    Set Re = New RegExp
        Re.IgnoreCase = True
        Re.Pattern = "<head[^>]*>"
    
    xmlResponse = Re.Replace(objXML.responseText, "$&<base href=""http://notimportant.com/"" />")