封装请求方法 @param url 请求路径 @param method 请求方法 @param param 请求参数 @return 请求方法 @throws UnsupportedEncodingException
(String url, String method, String param)
| 202 | * @throws UnsupportedEncodingException |
| 203 | */ |
| 204 | private HttpUriRequest parseHttpRequest(String url, String method, |
| 205 | String param) throws UnsupportedEncodingException { |
| 206 | // 处理url |
| 207 | url = parseUrl(url); |
| 208 | ReportUtil.log("method:" + method); |
| 209 | ReportUtil.log("url:" + url); |
| 210 | ReportUtil.log("param:" + param.replace("\r\n", "").replace("\n", "")); |
| 211 | if ("post".equalsIgnoreCase(method)) { |
| 212 | // 封装post方法 |
| 213 | HttpPost postMethod = new HttpPost(url); |
| 214 | postMethod.setHeaders(publicHeaders); |
| 215 | HttpEntity entity = new StringEntity(param, "UTF-8"); |
| 216 | postMethod.setEntity(entity); |
| 217 | return postMethod; |
| 218 | } else if ("upload".equalsIgnoreCase(method)) { |
| 219 | HttpPost postMethod = new HttpPost(url); |
| 220 | @SuppressWarnings("unchecked") |
| 221 | Map<String, String> paramMap = JSON.parseObject(param, HashMap.class); |
| 222 | MultipartEntity entity = new MultipartEntity(); |
| 223 | for (String key : paramMap.keySet()) { |
| 224 | String value = paramMap.get(key); |
| 225 | Matcher m = funPattern.matcher(value); |
| 226 | if (m.matches() && m.group(1).equals("bodyfile")) { |
| 227 | value = m.group(2); |
| 228 | entity.addPart(key, new FileBody(new File(value))); |
| 229 | } else { |
| 230 | entity.addPart(key, new StringBody(paramMap.get(key))); |
| 231 | } |
| 232 | } |
| 233 | postMethod.setEntity(entity); |
| 234 | return postMethod; |
| 235 | } else { |
| 236 | // 封装get方法 |
| 237 | HttpGet getMethod = new HttpGet(url); |
| 238 | getMethod.setHeaders(publicHeaders); |
| 239 | return getMethod; |
| 240 | }// delete put.... |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * 格式化url,替换路径参数等。 |