Reputation: 67
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.
Upvotes: 0
Views: 215
Reputation: 279940
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
.
Upvotes: 1