Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fetch another url content

Status
Not open for further replies.

marcela24

Programmer
Feb 17, 2003
72
NL
How can i fecth the content of an url (thats not stored in my web server) and return it to my asp page ( the one who is responsible for calling this url)?
 
Assuming you want the end output (not the source ASP code) of a remote page, use the MSXML2.XMLHTTP object.

<script runat=&quot;server&quot; language=&quot;javascript&quot;>
try{
var url = new String(Request.QueryString(&quot;url&quot;));
if (url != &quot;undefined&quot;){
var http = Server.CreateObject(&quot;MSXML2.XMLHTTP&quot;);
http.open(&quot;GET&quot;, url, false);
http.send();
Response.write(http.responseText);
// Above line is where you'd do something else with the
// returned resource - say, xml or regex parse it.
http = null;
}else Response.write(&quot;add url param to querystring.&quot;);
}catch(e){Response.write(e.description);}
</script>

codestorm
Newbie Life Member.
Fire bad. Tree pretty. - Buffy
<insert witticism here>
 
That's work fine for me! Thenk you.
Now i have another problem...
As i said before i want to fetch the output of a page, using asp code. This XMLHTTP componet works fine to do it, but the information i want is not appearing because i need the session. How can i get the session of a page and pass it to the next page on the sequence iam trying to reach?

For example, I do the login very well and it opens the next page. Futher i want to go to a second page that is not showing all the data because i don´t have the session. How can i make this work?
 
Ahhh, that is trickier. I'm having trouble myself at the moment getting cookies through the XMLHTTP object.

Someone told me to just use the getResponseHeader and getAllResponseHeaders methods, but the cookies don't appear using those methods.

To rewind a bit, in order to request a second page after passing form data with a first XMLHTTP request sent to a login page (takes a breath), you'll need to retrieve the cookie the login page response will generally send - this is what I'm having difficulty doing.

You /could/ instead simply pass hidden form variables about to maintain state, but I personally consider that about as tacky as relying on IIS Session (which I might add should I believe also be passed as a cookie to the XMLHTTP object anyway - an ASPSessionID handle to the server-side Session).

codestorm
Newbie Life Member.
Fire bad. Tree pretty. - Buffy
<insert witticism here>
 
I found something.
IHTMLDocument2 interface has a Cookie property that returns a list of all cookies specific to the current page.These cookies are then sent to the server in the HTTP request headers.

And theres an example:
Public Sub Send()
On Error GoTo eh
Dim oXMLHTTP As MSXML.XMLHTTPRequest
Dim objDoc As IHTMLDocument2
Set oXMLHTTP = New MSXML.XMLHTTPRequest
oXMLHTTP.open &quot;POST&quot;, mServerURL, False

'send any document cookies
Set objDoc = UserControl.Parent
If Len(objDoc.cookie) > 0 Then
oXMLHTTP.setRequestHeader &quot;Cookie&quot;, objDoc.cookie
End If
'send request
oXMLHTTP.Send moRequestDoc
Set moRequestDoc = Nothing 'reinit
If oXMLHTTP.responseXML.xml = &quot;&quot; Then
MsgBox &quot;No valid reponse returned. &quot; & _
vbCrLf & &quot;Response text is: &quot; & _
oXMLHTTP.responseText, vbCritical, &quot;XIR Client&quot;
Else
'retrieve response
Set moResponseDoc = oXMLHTTP.responseXML
End If
Exit Sub
eh:
MsgBox &quot;Error sending request: &quot; & vbCrLf & Err.Description
End Sub

But i can´t make it work iam receiving error messages.
Do you know what´s wrong with this example? Or how can i use the IHTMLDocument2?
 
Well, at a quick object browse through MSXML2 (v4) I can't see an IHTMLDocument interface.

Are you sure it doesn't belong to another DLL?

And what is the error? Invalid ProgID?

codestorm
Newbie Life Member.
Fire bad. Tree pretty. - Buffy
<insert witticism here>
 
Ahhh, that's all it is (re cookies) - you have to use ServerXMLHTTP rather than XMLHTTP to retrieve the cookies.

codestorm
Newbie Life Member.
Fire bad. Tree pretty. - Buffy
<insert witticism here>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top