2015-04-02 39 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); 

你如何解決問題了嗎?