| | |
| | | e.printStackTrace(); |
| | | } |
| | | return result; |
| | | }/*** |
| | | * get请求(带参数) |
| | | * @param url |
| | | * @return String |
| | | */ |
| | | public static String getReq1(String url, Map<String, String> params) { |
| | | String result = null; |
| | | try { |
| | | URIBuilder uriBuilder = new URIBuilder(url); |
| | | Iterator maplist = params.entrySet().iterator(); |
| | | while (maplist.hasNext()) { |
| | | Map.Entry<String, String> map = (Map.Entry<String, String>) maplist.next(); |
| | | uriBuilder.addParameter(map.getKey(), map.getValue()); |
| | | } |
| | | CloseableHttpClient client = HttpClientBuilder.create().build(); |
| | | HttpPost get = new HttpPost(uriBuilder.build()); |
| | | // todo 验签 |
| | | get.setHeader("Authorization", "close"); |
| | | get.setHeader("Content-Type", "application/json"); |
| | | get.setHeader("Accept", "application/json"); |
| | | get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36"); |
| | | HttpResponse response = client.execute(get); |
| | | result = EntityUtils.toString(response.getEntity(), "UTF-8"); |
| | | |
| | | } catch (URISyntaxException e) { |
| | | e.printStackTrace(); |
| | | } catch (ClientProtocolException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return result; |
| | | } |
| | | public static String postRequest(String url, Map<String, String> params) { |
| | | // 构造HttpClient的实例 |
| | |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | public static String postRequest1(String url, Map<String, Object> params) { |
| | | // 构造HttpClient的实例 |
| | | HttpClient httpClient = new HttpClient(); |
| | | // 创建POST方法的实例 |
| | | PostMethod postMethod = new PostMethod(url); |
| | | // 设置请求头信息 |
| | | postMethod.setRequestHeader("Authorization", "close"); |
| | | postMethod.setRequestHeader("Accept", "application/json"); |
| | | postMethod.addRequestHeader("Content-Type", "application/json"); |
| | | postMethod.addRequestHeader("Wechatpay-Serial", "application/json"); |
| | | postMethod.setRequestHeader("Connection", "close"); |
| | | // 添加参数 |
| | | for (Map.Entry<String, Object> entry : params.entrySet()) { |
| | | postMethod.addParameter(entry.getKey(), entry.getValue().toString()); |
| | | } |
| | | // 使用系统提供的默认的恢复策略,设置请求重试处理,用的是默认的重试处理:请求三次 |
| | | httpClient.getParams().setBooleanParameter("http.protocol.expect-continue", false); |
| | | // 接收处理结果 |
| | | String result = null; |
| | | try { |
| | | // 执行Http Post请求 |
| | | httpClient.executeMethod(postMethod); |
| | | // 返回处理结果 |
| | | result = postMethod.getResponseBodyAsString(); |
| | | } catch (HttpException e) { |
| | | // 发生致命的异常,可能是协议不对或者返回的内容有问题 |
| | | System.out.println("请检查输入的URL!"); |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | // 发生网络异常 |
| | | System.out.println("发生网络异常!"); |
| | | e.printStackTrace(); |
| | | } finally { |
| | | // 释放链接 |
| | | postMethod.releaseConnection(); |
| | | // 关闭HttpClient实例 |
| | | if (httpClient != null) { |
| | | ((SimpleHttpConnectionManager) httpClient.getHttpConnectionManager()).shutdown(); |
| | | httpClient = null; |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | public static String getRequest(String url, Map<String, String> params) { |
| | | // 构造HttpClient实例 |
| | | HttpClient client = new HttpClient(); |