How to emulate a real http request via cfhttp?
Asked Answered
T

2

7

I need to emulate a real http request via cfhttp. I was getting rss feed with ColdFusion, but tonight they started to block my request and send an index page in response instead of rss fead.

I added useragent for cfhttp, but it doesn't help.

Opera, Firefox and Chrome open feed correctly from the same computer.

Tenuto answered 31/5, 2010 at 8:27 Comment(0)
T
6

Yep, thanks. I sniffed all HTTP headers which browser sends to the site and then emulated them in cfhttp request. The solution is:

<cfhttp url="http://example.com/feed" 
useragent="Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.7 (KHTML, like Gecko) Chrome/5.0.391.0 Safari/533.7"
result="httpresult"
redirect="false"
>
<cfhttpparam type="header" name="HTTP_REFERER" value="http://example.com/feed/" >
<cfhttpparam type="header" name="Accept-Encoding" value="gzip,deflate,sdch" >
<cfhttpparam type="header" name="Proxy-Connection" value="keep-alive" >
<cfhttpparam type="header" name="Accept" value="application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5">
<cfhttpparam type="header" name="Accept-Language" value="en-US,en;q=0.8">
<cfhttpparam type="header" name="Accept-Charset" value="ISO-8859-1,utf-8;q=0.7,*;q=0.3">
<cfhttpparam type="cookie" name="some-cookie" value="1">

Tenuto answered 1/6, 2010 at 9:4 Comment(1)
For anyone who may find this in the future, I don't think that the header for referer should be a name of "http_referer" (which is how it appears in CF's CGI scope) but instead just "referer". It doesn't seem to have mattered in the OP's request, but it may be critical for others calling some server, if it really checks that header. (And don't let anything correct the typing of referer. While in english it's spelled with two r's, in the http spec it's spelled with one.)Rimarimas
A
4

I would guess that the site with the RSS feed is sniffing the User Agent still, and the CFHTTP one isn't set to one that the site is using. Use a HTTP Proxy Sniffer (ie Charles HTTP Proxy) to record the HTTP request of a browser that is displaying the RSS feed correctly, then try using CFHTTP with the same User Agent string as a previously successful request.

If it still doesn't work, use the 'proxyport' and 'proxyserver' attributes of CFHTTP to run the ColdFusion request through your HTTP sniffer and check to make sure the User Agent is being set correctly and compare to a working request.

Alula answered 31/5, 2010 at 12:26 Comment(2)
Also worth checking other headers, not just user-agent. (Maybe the remove server is looking for a cookie or accept-* headers.)Bateau
You may also want to try using the proxy on another server, in case they are blocking by IP.Indigence

© 2022 - 2024 — McMap. All rights reserved.