Home > Java > javaTutorial > Why Does My Java Program Get a 403 Forbidden Error from Google Search While My Browser Doesn't?

Why Does My Java Program Get a 403 Forbidden Error from Google Search While My Browser Doesn't?

Patricia Arquette
Release: 2024-12-12 13:54:11
Original
368 people have browsed it

Why Does My Java Program Get a 403 Forbidden Error from Google Search While My Browser Doesn't?

403 Forbidden Encountered with Java but Not Web Browsers

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")));

...
Copy after login

Note:

  1. SSL Handling: SSL is handled transparently, as seen in the exception stacktrace.
  2. Result Amount Retrieval: Obtaining the exact result amount requires additional steps, including fetching cookies and parsing redirect tokens.
  3. Example Code: The provided code demonstrates the complete process, resulting in the correct number of results.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template