"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Correctly Set the User-Agent in Java URLConnection?

How to Correctly Set the User-Agent in Java URLConnection?

Published on 2024-11-16
Browse:714

How to Correctly Set the User-Agent in Java URLConnection?

Setting User Agent of a Java URLConnection

When attempting to parse a webpage using Java with URLConnection and setting the user-agent to a specified value, an additional "Java/1.5.0_19" may be appended to the end. This arises due to a limitation in older versions of Java.

Solution (Java 1.6.30 and Newer)

In Java 1.6.30 and newer, this issue has been resolved. Setting the user agent using setRequestProperty("User-Agent", "Mozilla ...") now works correctly without appending the Java version.

Verification

To verify this, you can listen on a port using netcat, which displays the raw HTTP headers of incoming requests. Without setting the user agent, the headers will show:

GET /foobar HTTP/1.1
User-Agent: Java/1.6.0_30
Host: localhost:8080
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive

When setting the user agent, the headers will instead show:

GET /foobar HTTP/1.1
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
Host: localhost:8080
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive

Example Code (Java 1.6.30 )

The following code example demonstrates how to correctly set the user agent:

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());
    }

}
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3