1、导入依赖
< dependencies> < dependency> < groupId> org.apache.httpcomponents</ groupId> < artifactId> httpclient</ artifactId> < version> 4.5.3</ version> </ dependency> < dependency> < groupId> org.slf4j</ groupId> < artifactId> slf4j-log4j12</ artifactId> < version> 1.7.25</ version> </ dependency>
</ dependencies>
2、CrawlerFirst
package com. atguigu. crawler. test ;
import org. apache. http. HttpEntity ;
import org. apache. http. client. methods. CloseableHttpResponse ;
import org. apache. http. client. methods. HttpGet ;
import org. apache. http. impl. client. CloseableHttpClient ;
import org. apache. http. impl. client. HttpClients ;
import org. apache. http. util. EntityUtils ;
import java. io. IOException ;
public class CrawlerFirst { public static void main ( String [ ] args) throws IOException { CloseableHttpClient httpClient = HttpClients . createDefault ( ) ; HttpGet httpGet = new HttpGet ( "https://blog.csdn.net/m0_65152767?spm=1010.2135.3001.5343" ) ; CloseableHttpResponse response = httpClient. execute ( httpGet) ; if ( response. getStatusLine ( ) . getStatusCode ( ) == 200 ) { HttpEntity httpEntity = response. getEntity ( ) ; String content = EntityUtils . toString ( httpEntity, "UTF-8" ) ; System . out. println ( content) ; } }
}