<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<!-- <scope>test</scope>-->
</dependency>
log4j.properties文件
log4j.rootLogger=INFO,A3,STDOUT
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=[%p] [%l] %10.10c - %m%n
log4j.appender.A3=org.apache.log4j.RollingFileAppender
log4j.appender.A3.file=logs/server.log
log4j.appender.A3.MaxFileSize=1024KB
log4j.appender.A3.MaxBackupIndex=10
log4j.appender.A3.layout=org.apache.log4j.PatternLayout
log4j.appender.A3.layout.ConversionPattern=\n\n[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
1、get无参数
public class HttpGetTest {
public static void main(String[] args) {
CloseableHttpClient aDefault = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(“http://www.itcast.cn”);
CloseableHttpResponse response = null;
try {
response = aDefault.execute(httpGet);
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity,“utf8”);
System.out.println(content.length());
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
aDefault.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2、get有参数
public static void main(String[] args) throws IOException, URISyntaxException {
//1打开浏览器
CloseableHttpClient http = HttpClients.createDefault();
//2输入网址
URIBuilder uriBuilder = new URIBuilder("http://resource.boxuegu.com/booklist/find.html");
uriBuilder.setParameter("search","java");
uriBuilder.setParameter("enc","utf-8");
HttpGet httpGet = new HttpGet(uriBuilder.build());
//设置请求信息
RequestConfig config = RequestConfig.custom().setConnectTimeout(1000)//创建连接的最长时间,单位是毫秒
.setConnectionRequestTimeout(500)//设置获取连接的最长时间,单位毫秒
.setSocketTimeout(10*1000)//设置数据传输最长时间,单位毫秒
.build();
httpGet.setConfig(config);
//HttpGet httpGet = new HttpGet("https://search.jd.com/Search");
//System.out.println("发起请求"+httpGet);
//3回车
CloseableHttpResponse response = http.execute(httpGet);
//4解析响应数据
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity, "utf8");
System.out.println(content);
}
}
3、post无参
//无参数
public static void main(String[] args) {
CloseableHttpClient aDefault = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(“http://www.itcast.cn”);
CloseableHttpResponse response = null;
try {
response = aDefault.execute(httpPost);
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity,“utf8”);
System.out.println(content.length());
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
aDefault.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4、post有参数
public static void main(String[] args) throws UnsupportedEncodingException {
CloseableHttpClient aDefault = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(“http://yun.itheima.com/search”);
//list集合
List params = new ArrayList();
params.add(new BasicNameValuePair(“keys”,“java”)) ;
UrlEncodedFormEntity entity1 = new UrlEncodedFormEntity(params,“utf8”);
httpPost.setEntity(entity1);
CloseableHttpResponse response = null;
try {
response = aDefault.execute(httpPost);
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity,"utf8");
System.out.println(content.length());
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
aDefault.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
5、连接池
public static void main(String[] args) {
PoolingHttpClientConnectionManager mg = new PoolingHttpClientConnectionManager();
mg.setMaxTotal(100);//最大连接数
mg.setDefaultMaxPerRoute(10);//每个主机最大链接数
doGet(mg);
}
private static void doGet(PoolingHttpClientConnectionManager mg) {
CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(mg).build();
HttpGet httpGet = new HttpGet("http://www.baidu.com");
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httpGet);
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
String s = EntityUtils.toString(entity,"utf8");
System.out.println(s.length());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(response!=null){
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}