monkey命令執行後產生的日誌分析
阿新 • • 發佈:2021-12-08
1 URIBuilder ub = new URIBuilder(); 2 URI uri = ub.setScheme("http") 3 .setHost("www.google.com") 4 .setPath("/search") 5 .setParameter("q", "程式設計狗的部落格") 6 .setParameter("btnG", "Google Search") 7 .setParameter("aq", "f")8 .setParameter("oq", "") 9 .build(); 10 System.out.println(uri);
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.12</Mven依賴version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.72</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency>
1 import com.alibaba.fastjson.JSON; 2 import com.alibaba.fastjson.JSONObject; 3 import org.apache.commons.lang.StringEscapeUtils; 4 import org.apache.http.HttpEntity; 5 import org.apache.http.HttpStatus; 6 import org.apache.http.NameValuePair; 7 import org.apache.http.client.entity.UrlEncodedFormEntity; 8 import org.apache.http.client.methods.CloseableHttpResponse; 9 import org.apache.http.client.methods.HttpGet; 10 import org.apache.http.client.methods.HttpPost; 11 import org.apache.http.client.methods.HttpRequestBase; 12 import org.apache.http.client.utils.URIBuilder; 13 import org.apache.http.entity.StringEntity; 14 import org.apache.http.impl.client.CloseableHttpClient; 15 import org.apache.http.impl.client.HttpClients; 16 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; 17 import org.apache.http.message.BasicNameValuePair; 18 import org.apache.http.util.EntityUtils; 19 import java.util.*; 20 import java.io.IOException; 21 import java.net.URISyntaxException; 22 import java.util.ArrayList; 23 import java.util.Map; 24 25 26 27 public final class HttpClientUtils { 28 29 private static volatile PoolingHttpClientConnectionManager cm;// 連線池配置管理 30 31 private static String EMPTY_STR = ""; 32 33 private static String UTF_8 = "UTF-8"; 34 35 /** 36 * 配置初始化 37 */ 38 private static synchronized void init() { 39 if (cm == null) { 40 cm = new PoolingHttpClientConnectionManager(); 41 cm.setMaxTotal(50);// 整個連線池最大連線數 42 cm.setDefaultMaxPerRoute(25);// 每路由最大連線數,預設值是2 43 } 44 } 45 46 /** 47 * 通過連線池獲取HttpClient 48 */ 49 private static CloseableHttpClient getHttpClient() { 50 // 初始化 51 init(); 52 // 返回httpclient 53 return HttpClients.custom().setConnectionManager(cm).build(); 54 } 55 56 /** 57 * get請求 58 */ 59 public static HttpEntity httpGetRequest(String url) throws URISyntaxException, IOException { 60 61 return httpGetRequest(url,null,null); 62 } 63 64 /** 65 * get請求,引數 66 */ 67 public static HttpEntity httpGetRequest(String url, Map<String, Object> params) throws URISyntaxException, IOException { 68 69 return httpGetRequest(url,null,params); 70 } 71 72 73 /** 74 * get請求,請求頭 75 */ 76 public static HttpEntity httpGetRequest(String url, HashMap<String, Object> headers) throws URISyntaxException, IOException { 77 78 return httpGetRequest(url,headers,null); 79 } 80 81 82 /** 83 * get請求,請求頭 + 引數 84 */ 85 public static HttpEntity httpGetRequest(String url, Map<String, Object> headers, Map<String, Object> params) throws URISyntaxException, IOException { 86 URIBuilder ub = new URIBuilder(url); 87 88 if(!Objects.isNull(params)){ 89 ArrayList<NameValuePair> pairs = covertParams2NVPS(params); 90 ub.setParameters(pairs); 91 } 92 93 HttpGet httpGet = new HttpGet(ub.build()); 94 if(!Objects.isNull(headers)){ 95 for (Map.Entry<String, Object> header : headers.entrySet()) { 96 httpGet.addHeader(header.getKey(), String.valueOf(header.getValue())); 97 } 98 } 99 100 return getEntity(httpGet); 101 } 102 103 104 /** 105 * post請求 106 */ 107 public static HttpEntity httpPostRequest(String url) throws IOException { 108 return httpPostRequest(url, null, null, null); 109 } 110 111 /** 112 * post請求,請求頭 113 */ 114 public static HttpEntity httpPostRequest(String url, HashMap<String, Object> headers) throws IOException { 115 return httpPostRequest(url, headers, null, null); 116 } 117 118 /** 119 * post請求,json體 120 */ 121 public static HttpEntity httpPostRequest(String url, String jsonStrBody) throws IOException { 122 return httpPostRequest(url, null, null, jsonStrBody); 123 } 124 125 126 /** 127 * post請求,formdata引數 128 */ 129 public static HttpEntity httpPostRequest(String url, Map<String, Object> params) throws IOException { 130 return httpPostRequest(url, null, params, null); 131 } 132 133 /** 134 * post請求,請求頭 + json體 135 */ 136 public static HttpEntity httpPostRequest(String url, HashMap<String, Object> headers, String jsonStrBody) throws IOException { 137 return httpPostRequest(url, headers, null, jsonStrBody); 138 } 139 140 141 /** 142 * post請求,請求頭 + formdata引數 143 */ 144 public static HttpEntity httpPostRequest(String url, HashMap<String, Object> headers, Map<String, Object> params) throws IOException { 145 return httpPostRequest(url, headers, params, null); 146 } 147 148 149 /** 150 * post請求彙總方法:請求頭,formdata引數,jsonbody引數 151 */ 152 private static HttpEntity httpPostRequest(String url, Map<String, Object> headers, Map<String, Object> params, String jsonStrBody) throws IOException { 153 HttpPost httpPost = new HttpPost(url); 154 155 if(!Objects.isNull(headers)){ 156 for (Map.Entry<String, Object> param : headers.entrySet()) { 157 httpPost.addHeader(param.getKey(), String.valueOf(param.getValue())); 158 } 159 } 160 if (!Objects.isNull(params)){ 161 ArrayList<NameValuePair> pairs = covertParams2NVPS(params); 162 httpPost.setEntity(new UrlEncodedFormEntity(pairs, UTF_8)); 163 } 164 if(!Objects.isNull(jsonStrBody)){ 165 StringEntity se = new StringEntity(jsonStrBody, UTF_8); 166 se.setContentType("text/json"); 167 httpPost.setEntity(se); 168 } 169 return getEntity(httpPost); 170 } 171 172 /** 173 * 處理http請求,獲取HttpEntity 174 */ 175 private static HttpEntity getEntity(HttpRequestBase request) throws IOException { 176 CloseableHttpClient httpClient = getHttpClient(); 177 CloseableHttpResponse response = httpClient.execute(request); 178 int statusCode = response.getStatusLine().getStatusCode(); 179 if (statusCode != HttpStatus.SC_OK) { 180 request.abort(); 181 } 182 HttpEntity entity = response.getEntity(); 183 // response.close(); 184 // httpClient.close(); 185 return entity; 186 } 187 188 189 /** 190 * 處理Http請求,獲取String結果 191 */ 192 public static String getResultOfString(HttpEntity httpEntity) throws IOException { 193 String result = EntityUtils.toString(httpEntity, UTF_8); 194 return result; 195 } 196 197 /** 198 * 處理Http請求,獲取JSONObject結果 199 */ 200 public static JSONObject getResultOfJSONObject(HttpEntity httpEntity) throws IOException { 201 String result = EntityUtils.toString(httpEntity, UTF_8); 202 result = StringEscapeUtils.unescapeJava(result); 203 JSONObject jsonObject = JSON.parseObject(result); 204 return jsonObject; 205 } 206 207 208 /** 209 * 轉換成http引數對 210 * 211 * @param params 212 * @return 213 */ 214 private static ArrayList<NameValuePair> covertParams2NVPS(Map<String, Object> params) { 215 ArrayList<NameValuePair> pairs = new ArrayList<>(); 216 for (Map.Entry<String, Object> param : params.entrySet()) { 217 pairs.add(new BasicNameValuePair(param.getKey(), String.valueOf(param.getValue()))); 218 } 219 return pairs; 220 } 221 }HttClient工具類