HttpClient版本
<!-- httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
使用方法步骤
get请求
第一步:把HttpClient使用的jar包添加到工程中。
第二步:创建一个HttpClient的测试类
第三步:创建测试方法。
第四步:创建一个HttpClient对象
第五步:创建一个HttpGet对象,需要制定一个请求的url
第六步:执行请求。
第七步:接收返回结果。HttpEntity对象。
第八步:取响应的内容。
第九步:关闭HttpGet、HttpClient。
测试代码
@Test
public void testHttpGet() throws ClientProtocolException, IOException {
// 把HttpClient使用的jar包添加到工程中。
// 创建一个HttpClient的测试类
// 创建测试方法。
// 创建一个HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个HttpGet对象,需要制定一个请求的url
HttpGet httpGet = new HttpGet("http://www.itmayiedu.com");
// 执行请求,获得response
CloseableHttpResponse response = httpClient.execute(httpGet);
// 接收返回结果。转换成HttpEntity对象。
HttpEntity httpEntity = response.getEntity();
// 使用EntityUtils类toString()取响应的内容
String string = EntityUtils.toString(httpEntity);
System.out.println(string);
// 关闭Closeable的对象,response和httpClient
response.close();
httpClient.close();
}
结果
<!DOCTYPE HTML>
<!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html>
<!--<![endif]-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0,user-scalable=no,minimal-ui">
<title>蚂蚁课堂-中国专业的IT职业在线教育平台</title>
<meta name="author" content="http://www.itmayiedu.com" />
<meta name="keywords" content="蚂蚁课堂官网,Java学习视频,Java开发教程,移动开发,IT技能培训,php开发教程,web前端开发,在线编程学习,蚂蚁课堂官方交流群:124836778" />
<meta name="description" content="蚂蚁课堂是一家专门为IT学习者提供就业面试培训,编程教程分享,技术博客分享,技术疑难解答等诸多服务综合性教育网站。网站包含了Java视频教程,Java架构师视频教程,前端开发,Android开发,ios开发,游戏开发,设计交互课程等。包含了诸多领域,也是业内饺佼领先者!" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style"
content="black-translucent" />
<meta content="telephone=no" name="format-detection" />
<link rel="shortcut icon" href="http://www.itmayiedu.com/favicon.ico" type="image/x-icon">
<link rel="stylesheet" type="text/css"
href="http://www.itmayiedu.com/static/inxweb/css/reset.css?t=2017-07-27">
<link rel="stylesheet" type="text/css"
href="http://www.itmayiedu.com/static/inxweb/css/theme.css?t=2017-07-27">
<link rel="stylesheet" type="text/css"
href="http://www.itmayiedu.com/static/inxweb/css/global.css?t=2017-07-27">
<link rel="stylesheet" type="text/css"
href="http://www.itmayiedu.com/static/inxweb/css/web.css?t=2017-07-27">
<link rel="stylesheet" type="text/css"
href="http://www.itmayiedu.com/static/inxweb/css/adaptive.css?t=2017-07-27">
<link href="http://www.itmayiedu.com/static/inxweb/css/mw_320_768.css?t=2017-07-27" rel="stylesheet"
type="text/css"
media="screen and (min-width: 320px) and (max-width: 768px)">
<!--[if lt IE 9]><script src="http://www.itmayiedu.com/static/common/html5.js"></script><![endif]-->
<script type="text/javascript"
src="http://www.itmayiedu.com/static/common/jquery-1.7.2.min.js?t=2017-07-27"></script>
<script type="text/javascript" src="http://www.itmayiedu.com/static/common/webutils.js?t=2017-04-30"></script>
<script type="text/javascript"
src="http://www.itmayiedu.com/static/inxweb/header/header.js?t=2017-07-27"></script>
<script type="text/javascript" src="http://www.itmayiedu.com/static/inxweb/js/common.js?t=2017-07-27"></script>
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?b43c1c82b09cda6b125e6981fbde442c";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
<script>var baselocation = "http://www.itmayiedu.com";var keuploadSimpleUrl='http://www.itmayiedu.com/image/keupload?';var uploadServerUrl='http://www.itmayiedu.com';var uploadSimpleUrl="http://www.itmayiedu.com/image/gok4?";</script>
<meta name="baidu-site-verification" content="td22YrPILf" />
<script>
var theme_color = 'orange';
</script>
<script type="text/javascript">
$(document).ready(function() {
var i = setInterval(function() {
if ($(".in-wrap").next().is(":hidden")) {
clearTimeout(i);
$(".in-wrap").next().show();
//如果元素为隐藏,则将它显现
} else {
$(".in-wrap").next().html('');
$(".in-wrap").next().hide(); //如果元素为显现,则将其隐藏
}
}, 1);
});
</script>
</head>
省略body部分
post请求
第一步:创建一个httpClient对象
第二步:创建一个HttpPost对象。需要指定一个url
第三步:创建一个list模拟表单,list中每个元素是一个NameValuePair对象
第四步:需要把表单包装到Entity对象中。StringEntity
第五步:执行请求。
第六步:接收返回结果
第七步:关闭流。
测试代码
controller
@ResponseBody
@RequestMapping(value="/testpost", method=RequestMethod.POST)
public String testPost(String name, String pass) {
System.out.println(name);
System.out.println(pass);
return "ok";
}
Test
@Test
public void testHttpPost() throws ClientProtocolException, IOException {
// 创建一个httpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个HttpPost对象。需要指定一个url(url带.html是因为在web.xml中springmvc前端控制器配置了伪静态url拦截
HttpPost httpPost = new HttpPost("http://localhost:8082/testpost.html");
// 创建一个list模拟表单传入的参数,list中每个元素是一个NameValuePair对象
List<NameValuePair> formList = new ArrayList<>();
formList.add(new BasicNameValuePair("name", "张三"));
formList.add(new BasicNameValuePair("pass", "123456"));
// 需要把表单包装到Entity对象中。StringEntity
StringEntity entity = new UrlEncodedFormEntity(formList, "utf-8");
httpPost.setEntity(entity);
// 执行请求
CloseableHttpResponse response = httpClient.execute(httpPost);
// 接收返回结果
HttpEntity httpEntity = response.getEntity();
String string = EntityUtils.toString(httpEntity);
System.out.println(string);
// 关闭流
response.close();
httpClient.close();
}
结果
请求返回了OK,打印出了请求参数.