How to get the title of a page using httpxmlrequest - asynchronous

Is it possible to get the title of the page using the XMLHTTP object?
function getTitle(url)
Dim objRequest
Set objRequest = CreateObject("Msxml2.ServerXMLHTTP.3.0")
objRequest.open "GET", url , false
objRequest.Send
'getMsg = objRequest.status
'getMsg = objRequest.ResponseText
end function
I tried objRequest.title which didn't work.
UPDATE
Tried this which - probably not fail-proof - works for now:
f = objRequest.ResponseText
loc = Instr(f,"<title>")
loc2= Instr(f,"</title>")
getTitle = Mid(f,loc+7,loc2-loc-7)

You'd have to insert the result into a DOM in order for content to be recognized at HTML. Then you'd have to ask the DOM for the page's title.
What comes back from XHR is just a blob of text until you do something with it.

Related

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
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/"" />")

asp.net <form> tag getting stripped out of asp:literal

Okay... I know having nested tags is not officially supported. But stay with me on this one..
I have an ASP.NET web page with the standard <form runat=server> tag at the top. I am pulling in a Form (and associated fields) from a 3rd party source via HttpWebRequest on the server side (code behind). I can verify the data I get contains a <form> tag -- via a Trace statement. Then I assign the data to my literal like this:
Dim objRequest As System.Net.HttpWebRequest = System.Net.WebRequest.Create(url)
Dim objResponse As System.Net.WebResponse
objRequest.Method = "POST"
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData.ToString)
objRequest.ContentType = "application/x-www-form-urlencoded"
objRequest.ContentLength = byteArray.Length
Dim dataStream As Stream = objRequest.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
objResponse = objRequest.GetResponse()
Dim streamRead As New StreamReader(objResponse.GetResponseStream())
Dim strResponse As String = streamRead.ReadToEnd()
me.litCMSForm.Text = strResponse
When the page is rendered, somehow .NET removed the <form> tag that was within the literal.
I also tried assigning the variable "strResponse" to a public variable to be displayed and it too had the tag stripped out. And I tried assigning the variable to an asp:Label as well with no luck. And I tried assigning the value to the literal on the "PreRender" function with no success.
Any thoughts on this and why .NET is stripping out the <form> tag?
Thanks!
You're correct - nested form tags are not supported. What you can do is generate JavaScript that would pass the string to an HTML container outside of ASP.NET form or, better yet, output it to an independent iframe.
I had the same problem. I was able to work around the situation by adding another form before the one I really wanted to insert. It seems like ASP was only frisky enough to strip out the first firm it found.
Perhaps this would work for you:
me.litCMSForm.Text = "<form name='fakeOut'></form>" + strResponse
I actually had my form code be a little fancier (I gave it an action and an hidden input). I didn't continue to test to see what could be taken away because it worked.

how to run response.redirect on for loop

I Have process that should run atau go to url , but it should run on for loop :
For i = 0 To ds.Tables(0).Rows.Count - 1
idCustomer = ds.Tables(0).Rows(i)("House").ToString()
amt = ds.Tables(0).Rows(i)("OPR_BALANCE").ToString()
lang = "0"
aid = "000000"
Dim result As String = "http://soap.Services.com:2121/WS.aspx/?c=1&id=" + idCustomer + "&lang=" + lang + ""
Response.Redirect(result)
Next
but right now, based on my code, redirect to the url and , the progress is automaticly stop because its redirected.
is it possible to run process by redirectt to URL on for loop?
Redirecting inside a loop wont work. You need to call the service in other way.
Try using DownloadString method of WebClient. Extract result by this:
Dim client As New WebClient()
Dim result As String = client.DownloadString("http://soap.Services.com:2121/WS.aspx/?c=1&id=" + idCustomer + "&lang=" + lang )
you cannot do it with using response.redirect(). instead, you can use clientside code(JS) to do this logic. do your logic in JS and open up new pages with JS.
you can use jquery to call a webmethod. that webmethod can do the database stuff for you and return the data as json object. then you can do your logic in jquery to open new pages
I hope this helps
As soon as you call the Redirect you are done.... you can't redirect to mutiple pages....its illogical.....
Look this discussion : Redirect loop

I cannot seem to load an XML document using ASP (Classic), IIS6. Details inside

So I am writing a web application for use within my organization. The application requires that it know who the current user is. This is done by calling the Request.ServerVariables("AUTH_USER") function, which works great as long as 'Anonymous Access' is disabled (unchecked) and 'Integrated Windows Authentication' is enabled (checked) within IIS for this subweb.
Unfortunately by doing this I get an 'Access Denied' error when I hit the load method of the XML DOM.
Example code:
dim urlToXmlFile
urlToXmlFile = "http://currentwebserver/currentsubweb/nameofxml.xml"
dim xmlDom
set xmlDom = Server.CreateObject("MSXML2.DOMDocument")
xmlDom.async = false
xmlDom.load( urlToXmlFile ) ' <-- this is where I get the error!
I've looked everywhere and cannot find a solution. I should be able to load an XML file into the DOM regardless of the authentication method.
Any help would be appreciated. So far the only two solutions I can come up with are:
a) create a new subweb that JUST gets the current user name and somehow passes it back to my XML reading subweb.
b) open up security on the entire system to 'Everyone', which works but our IS department wouldn't care for that.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Here was my original code, which cause the access denied error:
dim urlToXml
urlToXml = "http://someserver/somesomeweb/nameofxml.xml"
dim xmlDom
set xmlDom = Server.CreateObject("MSXML2.DOMDocument")
xmlDom.loadXML( urlToXml )
dim xsl
set xsl = Server.CreateObject("MSXML2.DOMDocument")
xsl.async = false
xsl.load(server.MapPath("somexsl.xsl"))
Response.Write( xmlDom.transformNode(xsl) )
xmlDom.save( server.MapPath("accounting/somexml.xml") )
Now, here is my new code thanks to thomask:
dim urlToXml
urlToXml = "http://someserver/somesomeweb/nameofxml.xml"
set http = CreateObject("MSXML2.ServerXMLHTTP.3.0")
http.Open "GET", urlToXml, false
http.Send()
dim xmlDom
set xmlDom = Server.CreateObject("MSXML2.DOMDocument")
xmlDom.loadXML( http.responseXML.xml )
dim xsl
set xsl = Server.CreateObject("MSXML2.DOMDocument")
xsl.async = false
xsl.load(server.MapPath("somexsl.xsl"))
Response.Write( xmlDom.transformNode(xsl) )
xmlDom.save( server.MapPath("newxml.xml") )
Again thank you very much thomask.
You might wanna look at MSXML2.ServerXMLHTTP(.3.0 - 6.0) to specify the user credentials. If the Content-Type is configured correctly, ServerXMLHTTP should give you the DOMDocument in the responseXml property.
Dim http
Set http = CreateObject("MSXML2.ServerXMLHTTP.3.0")
http.Open("GET", "http://currentwebserver/currentsubweb/nameofxml.xml", false, "user", "pass")
http.Send()

HTTP GET Request, ASP - I'm lost!

Using VBScript with ASP I am trying to set up an HTTP GET Request which will visit a page which in turn generates a line of ASCII (non-HTML). I then want to extrapolate that ASCII line which will have 4 values delimited by semicolons back into 4 variables in my original ASP page so that I can take those values and do something with them.
This is the page I want to access with HTTP GET Request http://www.certigo.com/demo/request.asp. Three of the values are null here.
I don't know much/anything about ASP, so I have this:
Dim oXMLHTTP
Dim strStatusTest
Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP.3.0")
oXMLHTTP.Open "GET", "http://www.certigo.com/demo/request.asp", False
oXMLHTTP.Send
If oXMLHTTP.Status = 200 Then
strStatusText = oXMLHTTP.responseBody
End If
but obviously I haven't a clue what I'm doing because this isn't working at all. I would be totally unsurprised to learn that what I have here isn't going in the right direction. Please help!!
-Tracy
Your code should look like this:-
Function GetTextFromUrl(url)
Dim oXMLHTTP
Dim strStatusTest
Set oXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.3.0")
oXMLHTTP.Open "GET", url, False
oXMLHTTP.Send
If oXMLHTTP.Status = 200 Then
GetTextFromUrl = oXMLHTTP.responseText
End If
End Function
Dim sResult : sResult = GetTextFromUrl("http://www.certigo.com/demo/request.asp")
Note use ServerXMLHTTP from within ASP, the XMLHTTP component is designed for client side usage and isn't safe to use in the multithreaded environment such as ASP.
Try changing the oXMLHTTP.responseBody to oXMLHTTP.responseText and see if that works.
Refer to this web page if you need some more information on this technique:
http://classicasp.aspfaq.com/general/how-do-i-read-the-contents-of-a-remote-web-page.html.

Resources