java/js to get web page code

package test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpTest {
    private String u;
    private String encoding;

    public static void 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;
    }

    public void 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;
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325769574&siteId=291194637