Skip to content Skip to sidebar Skip to footer

Java - Not Getting Html Code From A Url

I want to get the html source code of https://www2.cslb.ca.gov/OnlineServices/CheckLicenseII/LicenseDetail.aspx?LicNum=872423 and for that I am using this method but I am not getti

Solution 1:

The server filters out Java's default User-Agent. This works:

publicstatic String getHTML(URL url) {
    try {
        finalURLConnectionurlConnection= url.openConnection();
        urlConnection.addRequestProperty("User-Agent", "Foo?");
        finalInputStreaminputStream= urlConnection.getInputStream();
        finalStringhtml= IOUtils.toString(inputStream);
        inputStream.close();
        return html;
    } catch (Exception e) {
        thrownewRuntimeException(e);
    }

Looks like the user agents are black listed. By default my JDK sends:

User-Agent: Java/1.6.0_26

Note that I'm using IOUtils class to simplify example, but the key things is:

urlConnection.addRequestProperty("User-Agent", "Foo?");

Post a Comment for "Java - Not Getting Html Code From A Url"