Web 瀏覽器成功時Java 程序收到403 禁止錯誤
問題:
一個Java旨在檢索給定Google 搜尋查詢的結果計數的程式返回403 Forbidden錯誤,而相同的查詢在 Web 瀏覽器中會產生結果。程式碼片段:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; public class DataGetter { public static void main(String[] args) throws IOException { getResultAmount("test"); } private static int getResultAmount(String query) throws IOException { BufferedReader r = new BufferedReader(new InputStreamReader(new URL("https://www.google.com/search?q=" + query).openConnection() .getInputStream())); String line; String src = ""; while ((line = r.readLine()) != null) { src += line; } System.out.println(src); return 1; } }
錯誤:
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: https://www.google.com/search?q=test at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source) at DataGetter.getResultAmount(DataGetter.java:15) at DataGetter.main(DataGetter.java:10)
解決方案:
出現此問題是因為Java的URLConnection 類別預設不會偽造實際的用戶代理程式。修改程式碼以設定使用者代理程式標頭可以解決此問題:
URLConnection connection = new URL("https://www.google.com/search?q=" + query).openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"); connection.connect(); BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));
此外,SSL 處理是透明的,如異常堆疊追蹤所示。
其他注意事項:
檢索結果計數涉及進一步的步驟,包括透過取得cookie 來偽造瀏覽器解析重定向令牌連結:
String cookie = connection.getHeaderField("Set-Cookie").split(";")[0]; Pattern pattern = Pattern.compile("content=\\"0;url=(.*?)\\""); Matcher m = pattern.matcher(response); if (m.find()) { String url = m.group(1); connection = new URL(url).openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"); connection.setRequestProperty("Cookie", cookie); connection.connect(); r = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8"))); sb = new StringBuilder(); while ((line = r.readLine()) != null) { sb.append(line); } response = sb.toString(); pattern = Pattern.compile("<div>
執行此完整程式碼會產生2930000000L 的結果。
以上是為什麼我的 Java 程式在抓取 Google 搜尋結果時會出現 403 禁止錯誤,而我的瀏覽器卻不會?的詳細內容。更多資訊請關注PHP中文網其他相關文章!