2015-04-02 42 views
3

如果我请求如下URL谷歌服务器如何区分浏览器和HtmlUnit?

http://www.google.com/recaptcha/api/noscript?k=MYPUBLICKEY 

我将获得验证码的老无脚本版本的谷歌街景数包含图像,这样

enter image description here

但是,如果我会做与HtmlUnit相同我会得到一些伪造版本的图像,像这样:

enter image description here

它总是发生:来自浏览器的真实世界街道号和来自HtmlUnit的黑色扭曲文本。公钥是一样的。

Google服务器如何区分浏览器和HtmlUnit?

的代码的HtmlUnit是如下:

final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_17); 
final HtmlPage page = webClient.getPage("http://www.google.com/recaptcha/api/noscript?k=" + getPublicKey()); 
HtmlImage image = page.<HtmlImage>getFirstByXPath("//img"); 
ImageReader imageReader = image.getImageReader(); 

过程是可观察到的使用Fiddler。

+0

最有可能的用户代理http标头。 – 2015-04-02 12:16:02

回答

0

如何为您的请求设置正确HeadersUser-Agent是这里的关键。

标题是后端可以获取客户端信息(Firefox,Chrome等)的方式,以及您的情况是什么?设置正确的标题例如。对于Firefox:

 conn.setRequestProperty("User-Agent", " Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0.1) Gecko/20100101 Firefox/8.0.1"); 
     conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); 

如果从我的代码中使用Apache HttpClient进行剪切,则需要根据需要进行调整。

0

我知道这是旧的文章,但是,好的办法是使用

WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER); 

你如何解决问题了吗?