接口自动化第四篇----应用工厂模式下的HttpClient请求
生活随笔
收集整理的這篇文章主要介紹了
接口自动化第四篇----应用工厂模式下的HttpClient请求
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
定義一個抽象類
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的三種請求方式執(zhí)行的基本url方法** @param: [url]* @return: java.lang.String*/abstract String execute(String url);/** @Description:分別對Get,Post的請求傳參,Get沒有參數(shù),Post含有參數(shù)** @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();//創(chuàng)建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;}@OverrideAbstractHttpClient 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();//創(chuàng)建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;}@OverrideAbstractHttpClient 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類型數(shù)據(jù)** */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();//創(chuàng)建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); // }//闖入的參數(shù),傳入的參數(shù)類型為"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;}@OverrideAbstractHttpClient 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*/@Testpublic 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);} }總結
以上是生活随笔為你收集整理的接口自动化第四篇----应用工厂模式下的HttpClient请求的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Shader Reflection Pr
- 下一篇: 账号密码都正确,使用jdbc连接远程服务