When attempting to retrieve the number of results for a Google search term using a Java program, developers may encounter a 403 Forbidden error while web browsers provide the correct results. This discrepancy arises from the lack of a user agent header in the Java program.
Solution:
To resolve the issue, it is necessary to set the user agent header in the Java program. Implement the following code snippet:
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"))); ...
Note:
The above is the detailed content of Why Does My Java Program Get a 403 Forbidden Error from Google Search While My Browser Doesn't?. For more information, please follow other related articles on the PHP Chinese website!