Here are examples:
Crawl the content of Baidu's homepage:
The code copy is as follows:
URL url = new URL("http://www.baidu.com");
HttpURLConnection urlCon=(HttpURLConnection)url.openConnection();
urlCon.setConnectTimeout(50000);
urlCon.setReadTimeout(300000);
DataInputStream fIn;
byte[] content = new byte[MAX_FILE_SIZE];
fIn = new DataInputStream(urlCon.getInputStream());
int size = 0,f_size = 0;
while((size = fIn.read(content,f_size,2048))> 0){
f_size += size;
}
In the code, we store the content of Baidu's homepage into a byte array. Of course, we can also store it in a file after we have the IO stream.