定义一个抽象类
package httpclientfactory;
import org.testng.annotations.Test;
/**
* @Author: ws
* @Description:
* @Date: Created in 10:44 2018/11/2
* @Modified By:
*/
public abstract class AbstractHttpClient {
/*
* @Description:这是关于httpclient的三种请求方式执行的基本url方法
*
* @param: [url]
* @return: java.lang.String
*/
abstract String execute(String url);
/*
* @Description:分别对Get,Post的请求传参,Get没有参数,Post含有参数
*
* @param: [params]
* @return: java.lang.Object
*/
abstract AbstractHttpClient setParams(Object params);
}
Get,post请求继承该抽象类
Get请求
package httpclientfactory;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
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 HttpclientGet extends AbstractHttpClient {
public String execute(String url){
CloseableHttpClient httpClient = null;
HttpGet httpGet = null;
try {
httpClient = HttpClients.createDefault();
//请求配置
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000)
.setConnectTimeout(20000)
.build();
//创建HttpGet实例
httpGet = new HttpGet(url);
//对HttpGet指定配置
httpGet.setConfig(requestConfig);
//启动httpget请求
CloseableHttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
String reponseInformation = EntityUtils.toString(entity,"UTF-8");
return reponseInformation;
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (httpGet != null) {
httpGet.releaseConnection();
}
if (httpClient != null ) {
httpClient.close();
}
}catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
@Override
AbstractHttpClient setParams(Object params) {
return null;
}
}
Post请求
package httpclientfactory;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpClientPostForm extends AbstractHttpClient{
/*
* HTTPCLIENT-POST请求:x-www-form-urlencoded 类型
*
* */
private Map<String, String> param;
public String execute(String url){
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
try {
httpClient = HttpClients.createDefault();
//请求配置
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000)
.setConnectTimeout(20000)
.build();
//创建HttpPost实例
httpPost = new HttpPost(url);
//对HttpPost指定配置
httpPost.setConfig(requestConfig);
List<NameValuePair> ps = new ArrayList<>();
for (String key : param.keySet()) {
NameValuePair nv = new BasicNameValuePair(key,param.get(key));
ps.add(nv);
}
UrlEncodedFormEntity requestEntity = new UrlEncodedFormEntity(ps);
httpPost.setEntity(requestEntity);
//启动httpPost请求
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String reponseInformation = EntityUtils.toString(entity,"UTF-8");
return reponseInformation;
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (httpPost != null) {
httpPost.releaseConnection();
}
if (httpClient != null ) {
httpClient.close();
}
}catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
@Override
AbstractHttpClient setParams(Object params) {
param = (Map<String, String>) params;
return this;
}
}
package httpclientfactory;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
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 HttpClientPostBody extends AbstractHttpClient{
/*
* HTTPCLIENT-POST请求:APPLICATION_JSON类型数据
*
* */
private String body;
public String execute(String url){
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
try {
httpClient = HttpClients.createDefault();
//请求配置
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000)
.setConnectTimeout(20000)
.build();
//创建HttpPost实例
httpPost = new HttpPost(url);
//对HttpPost指定配置
httpPost.setConfig(requestConfig);
// List<NameValuePair> ps = new ArrayList<>();
// for (String key : params.keySet()) {
// NameValuePair nv = new BasicNameValuePair(key,params.get(key));
// ps.add(nv);
// }
//闯入的参数,传入的参数类型为"APPLICATION_JSON"
StringEntity requestEntity = new StringEntity(body,ContentType.APPLICATION_JSON);
httpPost.setEntity(requestEntity);
//启动httpPost请求
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String reponseInformation = EntityUtils.toString(entity,"UTF-8");
return reponseInformation;
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (httpPost != null) {
httpPost.releaseConnection();
}
if (httpClient != null ) {
httpClient.close();
}
}catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
@Override
AbstractHttpClient setParams(Object params) {
body = params.toString();
return this;
}
}
工厂测试类
package httpclientfactory;
import org.testng.annotations.Test;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* @Author: ws
* @Description:
* @Date: Created in 15:31 2018/11/2
* @Modified By:
*/
public class HttpClientFactory {
/*
* @Description:
*
* @param: [type]
* @return: httpclientfactory.AbstractHttpClient
*/
public static AbstractHttpClient create(String type){
switch (type){
case "get":
return new HttpclientGet();
case "postForm":
return new HttpClientPostForm();
case "postBody":
return new HttpClientPostBody();
default:
return null;
}
}
/*
* @Description:
*
* @param: []
* @return: void
*/
@Test
public void test(){
//Get请求测试
String get = HttpClientFactory.create("get").execute("http://v.juhe.cn/weather/index?format=2&cityname=北京&key=9679a0703822fe67b9cff961ba197b02");
System.out.println(get);
//Post请求测试
Map<String,String> map = new HashMap<>();
map.put("subject","4");
map.put("model","a1");
map.put("testType","order");
map.put("key","5d5d6091ba01de0c27e84b4ab2fca5df");
String s1 = HttpClientFactory.create("postForm").setParams(map).execute("http://v.juhe.cn/jztk/query");
//String s1=post("http://v.juhe.cn/jztk/query",map);
System.out.println(s1);
}
}