Home >Java >javaTutorial >Why Am I Getting a 403 Forbidden Error When Connecting to Google with Java?

Why Am I Getting a 403 Forbidden Error When Connecting to Google with Java?

DDD
DDDOriginal
2024-12-28 06:43:28973browse

Why Am I Getting a 403 Forbidden Error When Connecting to Google with Java?

403 Forbidden when Connecting to Google with Java

When trying to connect to Google and retrieve information using Java, you may encounter a 403 Forbidden error, although the same request succeeds in web browsers. Here's why and how to fix it:

Java does not automatically set the "User-Agent" header, which browsers use to identify themselves. This can trigger a 403 error on websites that use this header to determine request legitimacy. To resolve this, manually set the "User-Agent" header as follows:

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

Additionally, processing the response requires further steps to emulate browser behavior, including fetching the cookie, parsing the redirect token link, and extracting the desired information. Here's the modified code:

String cookie = connection.getHeaderField("Set-Cookie").split(";")[0];
...
if (m.find()) {
    long amount = Long.parseLong(m.group(1).replaceAll(",", ""));
    return amount;
}

With these adjustments, the Java code should be able to successfully retrieve the search result amount from Google.

The above is the detailed content of Why Am I Getting a 403 Forbidden Error When Connecting to Google with Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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