Home >Java >javaTutorial >How to Set the User Agent of a Java URLConnection?

How to Set the User Agent of a Java URLConnection?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-06 07:21:12513browse

How to Set the User Agent of a Java URLConnection?

Setting User Agent of a Java URLConnection

When utilizing URLConnection to parse webpages in Java, it's crucial to set the user agent correctly. However, by default, the specified user agent is appended with "Java/1.5.0_19." This can be a concern when trying to conceal or impersonate a particular browser.

Clarification and Resolution

In Java versions 1.6.30 and later, the issue with user agent appending no longer exists, and setRequestProperty("User-Agent", "Mozilla ...") works as intended. To verify this, a port listener can be set up to capture HTTP headers.

When a request is sent without setRequestProperty being utilized, the HTTP headers will include:

User-Agent: Java/1.6.0_30

However, when setRequestProperty is used to set a specific user agent, the HTTP headers will reflect the specified value:

User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2

Example Code

The following code provides a complete example of how to set the user agent and retrieve the content type of a webpage:

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

public class TestUrlOpener {

    public static void main(String[] args) throws IOException {
        URL url = new URL("http://localhost:8080/foobar");
        URLConnection hc = url.openConnection();
        hc.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");

        System.out.println(hc.getContentType());
    }

}

The above is the detailed content of How to Set the User Agent of a Java URLConnection?. 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