模擬postman開發的http請求工具類
開發語言:JAVA
優點
1、開箱即用、非常方便
2、原生HTTP開發、對底層學習非常有幫助
3、依賴極少,簡單明了
4、支持度高、靈活
5、對自動化測試效率大大提高
可支持屬性
屬性 | 是否支持 | 備注 |
請求方式 | - | - |
POST | √ | |
GET | √ | |
PUT | √ | |
請求入參 | - | - |
Header設置 | √ | |
Body參數設置 | √ | 目前支持form-data方式的文件上傳、及json參數方式 |
使用環境:
* 運行環境:jdk1.8.0_131
* 依賴包:fastjson-1.2.7
調用方法集合
使用示例:
請求Get示例
public static void main(String[] args) throws IOException { String rs = PostmanUtils.sendGet("https://www.cnblogs.com/cheng2839"); System.out.println(rs); }
響應結果
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="referrer" content="origin" /> <meta http-equiv="Cache-Control" content="no-transform" /> <meta http-equiv="Cache-Control" content="no-siteapp" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <title>溫柔的星空,讓你感動 - 博客園</title> <link id="favicon" rel="shortcut icon" href="//common.cnblogs.com/favicon.ico?v=20200522" type="image/x-icon" /> 由於返回內容太多,省略其余行
請求POST示例(Body為json)
public static void main(String[] args) throws IOException { Map<String, Object> bodyMap = new HashMap<>(); bodyMap.put("userName", "張三"); bodyMap.put("userPass", "123456"); String rs = PostmanUtils.sendPostJson("https://www.xxx.com/mywebapp/login.action", bodyMap); System.out.println(rs); }
請求POST示例(Body為json,header中添加token)
Map<String, String> headerMap = new HashMap<>(); headerMap.put("token", "4e654ff507be927c8ea55a"); Map<String, Object> bodyMap = new HashMap<>(); bodyMap.put("userName", "張三"); bodyMap.put("userPass", "123456"); String rs = PostmanUtils.sendPostJson("https://www.xxx.com/mywebapp/login.action", headerMap, bodyMap); System.out.println(rs);
請求POST示例(Body為form-data文件)
Map<String, Object> bodyMap = new HashMap<>(); bodyMap.put("file", "D:/001.jpg"); String rs = PostmanUtils.sendPost("https://www.xxx.com/mywebapp/login.action", PostmanUtils.CONTENT_TYPE_FORM_DATA, null, bodyMap); System.out.println(rs);
等等不再此一一列舉
源碼如下:
package com.cheng2839.utils; import com.alibaba.fastjson.JSONObject; import javax.activation.MimetypesFileTypeMap; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.Map; /** * 實現postman中主要請求方式 * * 運行環境: * fastjson-1.2.7 * jdk1.8.0_131 * * @author cheng2839 * @date 2020年7月2日 */ public class PostmanUtils { private static final int CONNECTION_TIMEOUT = 5000; private static final int READ_TIMEOUT = 30000; private static final boolean USE_CACHE = false; private static final String BOUNDARY = "----------------1234567890987654321"; public static final String METHOD_POST = "POST"; public static final String METHOD_GET = "GET"; public static final String CONTENT_TYPE_FORM_DATA = "multipart/form-data;"; public static final String CONTENT_TYPE_JSON = "application/json;charset=UTF-8;"; private static HttpURLConnection CONNECTION; public static String sendGet(String url) throws IOException { return new String(send(url, METHOD_GET, CONTENT_TYPE_JSON, null, null)); } public static byte[] sendGet(String url, Map<String, String> headerMap) throws IOException { return send(url, METHOD_GET, CONTENT_TYPE_JSON, null, null); } public static String sendGet(String url, String contentType, Map<String, String> headerMap, Map<String, Object> bodyMap) throws IOException { return new String(send(url, METHOD_GET, contentType, headerMap, bodyMap)); } public static String sendGetJson(String url, Map<String, String> headerMap, Map<String, Object> bodyMap) throws IOException { return new String(send(url, METHOD_GET, CONTENT_TYPE_JSON, headerMap, bodyMap)); } public static String sendGetJson(String url, Map<String, Object> bodyMap) throws IOException { return new String(send(url, METHOD_GET, CONTENT_TYPE_JSON, null, bodyMap)); } public static String sendPost(String url, String contentType, Map<String, String> headerMap, Map<String, Object> bodyMap) throws IOException { return new String(send(url, METHOD_POST, contentType, headerMap, bodyMap)); } public static String sendPostJson(String url, Map<String, String> headerMap, Map<String, Object> bodyMap) throws IOException { return new String(send(url, METHOD_POST, CONTENT_TYPE_JSON, headerMap, bodyMap)); } public static String sendPostJson(String url, Map<String, Object> bodyMap) throws IOException { return new String(send(url, METHOD_POST, CONTENT_TYPE_JSON, null, bodyMap)); } public static byte[] send(String url, String method, String contentType, Map<String, String> headerMap, Map<String, Object> bodyMap) throws IOException { initHttpConnection(url, method); setHeader(contentType, headerMap); //setting json body if (bodyMap != null) { setBody(contentType, bodyMap); } return getResponse(); } private static byte[] getResponse() throws IOException { // read return message ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); InputStream inputStream = CONNECTION.getInputStream(); byte[] buffer = new byte[1024]; int len = 0; while (-1 != (len = inputStream.read(buffer))) { byteArrayOutputStream.write(buffer, 0, len); } inputStream.close(); return byteArrayOutputStream.toByteArray(); } private static String getContentTypeByFile(File file) { String fileName = file.getName(); String contentType = new MimetypesFileTypeMap().getContentType(file); if (isEmpty(contentType)) { contentType = "application/octet-stream"; } else { String[] endSuffixes = {".png", ".gif", ".ico", ".jpg", ".jpeg", ".jpe"}; String[] types = {"image/png", "image/gif", "image/image/x-icon", "image/jpeg"}; contentType = "application/octet-stream"; for (int i=0; i < endSuffixes.length; i++) { if (fileName.toLowerCase().endsWith(endSuffixes[i])) { contentType = types[Math.min(i, types.length-1)]; break; } } } return contentType; } private static boolean isEmpty(String s) { return null == s || "".equals(s.trim()); } }
備注:該類無法直接使用,因為此為作者原創,版權原因(需要付費1元,備注postman工具,即可發至郵箱),如有需要可留言聯系作者,請尊重作者時間和精力的耗費,見諒!
特別注意:備注不規范無法發送,請備注【郵箱+postman工具】,例如:123456@126.com,postman工具 並請在您付款后2小時內查收郵件