Есть следующие класса/возможности:
URL url1 = new URL("http://subdomain.domain:8080/path/to?param=value#hash");
URL url2 = new URL("http", "subdomain.domain", 8080, "path/to?param=value#hash");
URL subUrl = new URL(url0, "/path/to?param=value#hash");
URL url3 = new URI("http", "subdomain.domain", 8080, "some path/with spaces?param=value", "hash").toURL(); //если лень набирать символ пробела руками http://subdomain.domain:8080/some%20path/with%20spaces?param=value#hash"
Прямое чтение стримом с урла:
Дальше с помошью урлконекшин мы можем как читать данные так и передавать(например заполненную полученную форму с сайта).
java.net.URL:
URL url0 = new URL("http://subdomain.domain:8080");URL url1 = new URL("http://subdomain.domain:8080/path/to?param=value#hash");
URL url2 = new URL("http", "subdomain.domain", 8080, "path/to?param=value#hash");
URL subUrl = new URL(url0, "/path/to?param=value#hash");
URL url3 = new URI("http", "subdomain.domain", 8080, "some path/with spaces?param=value", "hash").toURL(); //если лень набирать символ пробела руками http://subdomain.domain:8080/some%20path/with%20spaces?param=value#hash"
import java.net.*;
import java.io.*;
public class ParseURL {
public static void main(String[] args) throws Exception {
URL aURL = new URL("http://example.com:80/docs/books/tutorial"
+ "/index.html?name=networking#DOWNLOADING");
System.out.println("protocol = " + aURL.getProtocol());
System.out.println("authority = " + aURL.getAuthority());
System.out.println("host = " + aURL.getHost());
System.out.println("port = " + aURL.getPort());
System.out.println("path = " + aURL.getPath());
System.out.println("query = " + aURL.getQuery());
System.out.println("filename = " + aURL.getFile());
System.out.println("ref = " + aURL.getRef());
}
}
Result:
protocol = http authority = example.com:80 host = example.com port = 80 path = /docs/books/tutorial/index.html query = name=networking filename = /docs/books/tutorial/index.html?name=networking ref = DOWNLOADING
Прямое чтение стримом с урла:
import java.net.*;
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.oracle.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
java.net.URLConnection group:
Данная группа классов служит для подключения конекшина, а потом использования его как для получения данных так и для отправки
try {
URL myURL = new URL("http://example.com/");
URLConnection myURLConnection = myURL.openConnection(); // в зависимости от протокола в ответе может быть HttpURLConnection,HttpsURLConnection или JarURLConnection
myURLConnection.connect();
}
catch (MalformedURLException e) {
// new URL() failed
// ...
}
catch (IOException e) {
// openConnection() failed
// ...
}
Дальше с помошью урлконекшин мы можем как читать данные так и передавать(например заполненную полученную форму с сайта).
Комментариев нет:
Отправить комментарий