定義一個抽象類
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);
}
}