package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
publicclass HttpTest {
private String u;
private String encoding;
publicstaticvoid main(String[] args) throws Exception {
HttpTest client = new HttpTest("http://www.baidu.com/", "UTF-8");
client.run();
}
public HttpTest(String u, String encoding) {
this.u = u;
this.encoding = encoding;
}
publicvoid run() throws Exception {
URL url = new URL(u); // According to the link (string format), generate a URL object
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();// 打开URL
BufferedReader reader = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream(), encoding)); // Get the input stream, that is, get the content of the web page
String line; // Read the data of the input stream and display while ((line = reader.readLine()) != null ) {
System.out.println(line);
}
}
}
js get:
var text = document.getElementsByTagName("html")[0].innerHTML;