HttpClient在HTTP协议接口测试中的使用
来源:互联网 发布:ubuntu 删除文件 编辑:程序博客网 时间:2024/06/05 10:43
一.GET请求: GET请求时,参数一般是写在链接上的,代码如下:
public static void get(String url){ CloseableHttpClient httpClient = null; HttpGet httpGet = null; try { httpClient = HttpClients.createDefault(); //设置请求和传输超时时间 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build(); httpGet = new HttpGet(url); httpGet.setConfig(requestConfig); CloseableHttpResponse response = httpClient.execute(httpGet); HttpEntity httpEntity = response.getEntity(); System.out.println(EntityUtils.toString(httpEntity,"utf-8")); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { if(httpGet!=null){ httpGet.releaseConnection(); } if(httpClient!=null){ httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } }}如果想把参数不写在链接上,单独的传进去,则可以这样:
public static void get(String url, Map<String, String> params){ CloseableHttpClient httpClient = null; HttpGet httpGet = null; try { httpClient = HttpClients.createDefault(); //设置请求和传输超时时间 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build(); String ps = ""; for (String pKey : params.keySet()) { if("".equals(pKey) || "".equals(params.get(pKey))){continue; } ps +="&"+pKey+"="+params.get(pKey); } if(!"".equals(ps)){ url = url + "?" + ps.substring(1,ps.length()); } httpGet = new HttpGet(url); httpGet.setConfig(requestConfig); CloseableHttpResponse response = httpClient.execute(httpGet); HttpEntity httpEntity = response.getEntity(); System.out.println(EntityUtils.toString(httpEntity,"utf-8")); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { if(httpGet!=null){ httpGet.releaseConnection(); } if(httpClient!=null){ httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } }}二. POST请求的提交方式,代码如下:
public void post(String url, Map<String, String> params){ CloseableHttpClient httpClient = null; HttpPost httpPost = null; try { httpClient = HttpClients.createDefault(); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build(); httpPost = new HttpPost(url); httpPost.setConfig(requestConfig); //表单方式 List<NameValuePair> ps = new ArrayList<NameValuePair>(); for (String pKey : params.keySet()) { ps.add(new BasicNameValuePair(pKey, params.get(pKey))); } httpPost.setEntity(new UrlEncodedFormEntity(ps)); //json方式// JSONObject jsonParam = new JSONObject(); // jsonParam.put("name", "admin");// jsonParam.put("pass", "123456");// StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8");//解决中文乱码问题 // entity.setContentEncoding("UTF-8"); // entity.setContentType("application/json"); // httpPost.setEntity(entity); CloseableHttpResponse response = httpClient.execute(httpPost); HttpEntity httpEntity = response.getEntity(); System.out.println(EntityUtils.toString(httpEntity,"utf-8")); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { if(httpPost!=null){ httpPost.releaseConnection(); } if(httpClient!=null){ httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } }}三. POST请求的RAW参数传递:
public void post(String url, String body){ CloseableHttpClient httpClient = null; HttpPost httpPost = null; try { httpClient = HttpClients.createDefault(); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build(); httpPost = new HttpPost(url); httpPost.setConfig(requestConfig); httpPost.setEntity(new StringEntity(body)); CloseableHttpResponse response = httpClient.execute(httpPost); HttpEntity httpEntity = response.getEntity(); System.out.println(EntityUtils.toString(httpEntity,"utf-8")); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { if(httpPost!=null){ httpPost.releaseConnection(); } if(httpClient!=null){ httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } }}
阅读全文
0 0
- HttpClient在HTTP协议接口测试中的使用
- HttpClient在HTTP协议接口测试中的使用
- java HttpClient在Http协议接口测试中的使用
- HttpClient在HTTP协议接口测试中的使用
- loadrunner 在HTTP接口测试中的应用
- loadrunner 在HTTP接口测试中的应用
- LoadRunner在HTTP接口测试中的应用
- Python在HTTP接口测试中的应用
- Python在HTTP接口测试中的应用
- Http协议-HttpClient的使用
- Http协议-HttpClient的使用
- http协议接口调用之HttpClient
- 使用HttpClient实现接口测试
- 使用httpClient进行接口测试
- 接口测试基础-HTTP协议
- 使用postMan实现接口测试以及接口自动化回归测试(http协议)
- 使用Http协议访问网络--HttpClient
- 使用httpclient实现http接口调用实例
- 通过例子学设计模式之--外观模式以及使用场景说明(C++实现)
- 重要文件写操作请用FileOutputStream,FileWriter有坑
- 实现在html上点击一个按钮出现一张照片
- 括号配对问题
- Linux netstat命令详解
- HttpClient在HTTP协议接口测试中的使用
- 轻钢别墅与旅游景区的完美结合
- smartctl工具学习网站
- 根据key取Jsonobject中value
- 自定义控件之按照数据百分比画圆环
- 为什么我推荐ImageJ?
- 【Owin 学习系列】1. 第一个 Owin 程序
- Java对象的强、软、弱和虚引用原理+结合ReferenceQueue对象构造Java对象的高速缓存器
- java-Android Error:Error converting bytecode to dex: Cause: Dex cannot parse version 52 byte code.