依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
代码
public class HttpClientDemo {
public static void main(String[] args) throws Exception {
// //get无参数请求
// httpGetNoParam();
// //get带参数请求
// httpGetParam();
// //post无参数请求
// httpPostNoParam();
// //post带参请求
// httpPostParam();
//下载图片
downloadPic("http://img10.360buyimg.com/n1/s450x450_jfs/t1/142896/38/7512/97310/5f5073c9E627505f2/201f731895155325.jpg");
}
/**
* httpclient带参post请求
*/
public static void httpPostParam() throws Exception {
//创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//发起get请求创建HttpGet对象
HttpPost httpPost = new HttpPost("http://www.itcast.cn");
//请求配置信息
RequestConfig requestConfig = getRequestConfig();
//给请求设置请求信息
httpPost.setConfig(requestConfig);
//申明List集合,封装表单中的参数
List<NameValuePair> params = new ArrayList<NameValuePair>();
//设置参数
params.add(new BasicNameValuePair("keys","Java"));
//创建表单entity对象,第一个参数就是封装好的表单数据,第二个参数就是编码
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params,"utf8");
//设置表单的entity对象到post请求中
httpPost.setEntity(formEntity);
//使用httpclient对象发起请求
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
//4获取数据
//判断状态码是否是200
if(response.getStatusLine().getStatusCode()==200){
String content = EntityUtils.toString(response.getEntity(), "utf8");
System.out.println(content.length());
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭资源
if(response!=null){
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
if(httpClient!=null){
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* httpclient无参post请求
*/
public static void httpPostNoParam(){
//1.打开浏览器,创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//2.输入网址,发起get请求创建HttpGet对象
HttpPost httpPost = new HttpPost("http://www.itcast.cn");
//请求配置信息
RequestConfig requestConfig = getRequestConfig();
//给请求设置请求信息
httpPost.setConfig(requestConfig);
//3.按回车,发起请求,返回响应,使用httpclient对象发起请求
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
//4.解析响应,获取数据
//判断状态码是否是200
if(response.getStatusLine().getStatusCode()==200){
String content = EntityUtils.toString(response.getEntity(), "utf8");
System.out.println(content.length());
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭资源
if(response!=null){
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
if(httpClient!=null){
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* httpclient带参get请求
*/
public static void httpGetParam() throws URISyntaxException {
//1.打开浏览器,创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//设置请求地址是:http://yun.itheima.com/search?keys=Java
//创建URLBuilder
URIBuilder uriBuilder = new URIBuilder("http://yun.itheima.com/search");
//设置参数
uriBuilder.setParameter("keys","Java");
//2.输入网址,发起get请求创建HttpGet对象
HttpGet httpGet = new HttpGet(uriBuilder.build());
//请求配置信息
RequestConfig requestConfig = getRequestConfig();
//给请求设置请求信息
httpGet.setConfig(requestConfig);
// System.out.println(httpGet);
//3.按回车,发起请求,返回响应,使用httpclient对象发起请求
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
//4.解析响应,获取数据
//判断状态码是否是200
if(response.getStatusLine().getStatusCode()==200){
String content = EntityUtils.toString(response.getEntity(), "utf8");
System.out.println(content.length());
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭资源
if(response!=null){
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
if(httpClient!=null){
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* httpclient无参get请求
*/
public static void httpGetNoParam(){
//1.打开浏览器,创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//2.输入网址,发起get请求创建HttpGet对象
HttpGet httpGet = new HttpGet("http://www.itcast.cn");
//请求配置信息
RequestConfig requestConfig = getRequestConfig();
//给请求设置请求信息
httpGet.setConfig(requestConfig);
//3.按回车,发起请求,返回响应,使用httpclient对象发起请求
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
//4.解析响应,获取数据
//判断状态码是否是200
if(response.getStatusLine().getStatusCode()==200){
String content = EntityUtils.toString(response.getEntity(), "utf8");
System.out.println(content.length());
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭资源
if(response!=null){
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
if(httpClient!=null){
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 下载图片
* @param imgUrl 图片链接
*/
public static void downloadPic(String imgUrl){
//打开浏览器,创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//输入网址,发起get请求创建HttpGet对象
HttpGet httpGet = new HttpGet(imgUrl);
//请求配置信息
RequestConfig requestConfig = getRequestConfig();
//给请求设置请求信息
httpGet.setConfig(requestConfig);
//按回车,发起请求,返回响应,使用httpclient对象发起请求
CloseableHttpResponse response = null;
OutputStream outputStream = null;
try {
response = httpClient.execute(httpGet);
if(response!=null){
//解析响应,获取数据
//判断状态码是否是200
if(response.getStatusLine().getStatusCode()==200){
//获取图片的后缀名
String imgLastName = imgUrl.substring(imgUrl.lastIndexOf("."));
//创建图片名
String picName = UUID.randomUUID().toString();
outputStream = new FileOutputStream(new File("C:\\Users\\yangdong\\Desktop\\images\\"+picName+imgLastName));
//下载图片
response.getEntity().writeTo(outputStream);
}
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭资源
if(outputStream!=null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(response!=null){
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
if(httpClient!=null){
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 请求配置信息
* @return
*/
public static RequestConfig getRequestConfig(){
//请求配置信息
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(1000) //创建连接的最长时间,单位毫秒
.setConnectionRequestTimeout(500) //设置获取连接的最长时间,单位是毫秒
.setSocketTimeout(10*1000) //设置数据传输的最长时间,单位是毫秒
.build();
return requestConfig;
}
}