The class URL represents a web resource and can be used to access it.
Here are some ways to define a URL:
// URL defined using an absolute path URL url = new URL("http://www.davisfiore.co.uk/"); // URL defined using a relaive path URL url = new URL("http://www.davisfiore.co.uk/"); URL myPage = new URL(url, "page.html"); // URL defined using protocol, host name and path URL url = new URL("http", "www.davisfiore.co.uk", "/page.html"); // URL defined using protocol, host name, port and path URL url = new URL("http", "www.davisfiore.co.uk", 80, "/page.html");
Here's the code to get a stream to access the content of a resource online:
URL url = new URL("http://www.davisfiore.co.uk"); URLConnection conn = url.openConnection(); InputStream is = conn.getInputStream();
Here's the code to escape the special characters:
URL url = new URL("http://www.davisfiore.co.uk?test=bla bla"); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); url = uri.toURL(); System.out.println(url);
It prints: http://www.davisfiore.co.uk?test=bla%20bla
That's because the class URI does perform escaping.
Copyright © 2013 Welcome to the website of Davis Fiore. All Rights Reserved.