Hi,
I am trying to access a certain web page from my server, but I want the
referring page to be a different one.
It seems the the get method accepts both a url and a “ref” parameter
that is is supposed to be the referring page, but when I try to send
that url as follows:
agent.get(url_to_get, “http://www.mininova.org”)
I get the following error:
ndefined method `uri’ for “http://www.mininova.org”:String.
What you need to do is create a Mechanize::URI object (or something like
that)
from the string “http://www.mininova.org”, and call #get() with that.
HTH,
Hi Konrad,
Thanks for the quick reply
I managed to get around that error by doing the following:
ref = WWW::Mechanize::Page.new(URI.parse(“http://www.mininova.org”),
{‘content-type’=>‘text/html’})
doc = agent.get(url_to_get, ref)
Problem is, when I sniff the package it seems that the referring url is
ignored and it is still set to localhost:3000. I’ve also tried
specifying a nil page (as I’ve seen in the mechanize get code itself)
but to no avail…
Any help would be appreciated.
BTW - removing the referrer altogether would also work for, albeit it’s
a less preferred option.
In the following line:
ref =
WWW::Mechanize::Page.new(“http://www.mininova.org’,{‘content-type’=>‘text/html’
})
the second apostrophe after mininova.org does not match the first one (”
vs. ').
When I try to run it with both sides contained by a double quote, i get
a message saying:
NoMethodError: undefined method `path’ for
“http://www.mininova.org”:String
That’s why I used the URI.parse method before.
this is really strange, are you sure that’s the exact syntax you used?
OK, I’m stupid
I don’t need mechanize to fake the referring page at all, but rails
itself in a redirect_to statement.
The flow is as such:
a. mechanize retrieves a page and scrapes it for a url
b. a redirect_to to that url is sent back to the cient
c. he goes and fetches the url (only right now he gets redirected since
his referrer is wrong.
I don’t think that you can do what you want this way, the referer is set
by
your browser not by the website you are browsing.
(Moreover, you are changing the referer from the request, not from the
response). And I don’t see why a server response
could have a referer header.
If you want to load a page with another referer from your rails app, try
to
use some javascript to change the referer of the
browser or make an ajax page request with the right referer (I think it
should be possible to set the header from the ajax
request).
Yes, it has finally dawned on me that what I’m trying to do is not
really possible this way. I doubt it can be done using ajax, though it’s
work a bit of research.
Anyway, thanks for putting me on the right track.
I don’t think that you can do what you want this way, the referer is set
by
your browser not by the website you are browsing.
(Moreover, you are changing the referer from the request, not from the
response). And I don’t see why a server response
could have a referer header.
If you want to load a page with another referer from your rails app, try
to
use some javascript to change the referer of the
browser or make an ajax page request with the right referer (I think it
should be possible to set the header from the ajax
request).
the XMLHttpRequest Object has a method setRequestHeader so we can change
the header from the request but i’m not sure that we can do an
xmlhttprequest to
a different domain than the current website.
And in javascript, document.referrer is read only.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.