简体   繁体   中英

java.net.URL to refer to a CSS

How do I create a java.net.URL to refer to a CSS? Is it posible?

I've tried several other ways like this to check if its a css page but it doesn't work (no errors tho, but it doesn't):

        int code = con.getResponseCode();
        String type = con.getContentType();

        con.disconnect();

        //returns null if the connection had problems or its does not contain css
        if (code != HttpURLConnection.HTTP_OK || !type.contains("stylesheet")) {
            return null;
        }

Any other posible solutions? Basically what I try to do is get the css page and print it.

Take the code below for example

URL url = new URL("http://cdn.sstatic.net/stackoverflow/all.css?v=e97b23688ee8"); // some css on this site
HttpURLConnection con = (HttpURLConnection) url.openConnection();

Scanner scanner = new Scanner(con.getInputStream());
while(scanner.hasNextLine()) 
    System.out.println(scanner.nextLine());

System.out.println(con.getContentType()); // prints text/css

You were probably looking for the wrong Content-Type .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM