| | |
| | | } |
| | | |
| | | private void init() { |
| | | X509TrustManager xtm = new X509TrustManager() { // 创建TrustManager |
| | | X509TrustManager xtm = new X509TrustManager() { // 创建TrustManager |
| | | public void checkClientTrusted(X509Certificate[] chain, |
| | | String authType) throws CertificateException { |
| | | } |
| | |
| | | } |
| | | }; |
| | | try { |
| | | // TLS1.0与SSL3.0基本上没有太大的差别,可粗略理解为TLS是SSL的继承者,但它们使用的是相同的SSLContext |
| | | // TLS1.0与SSL3.0基本上没有太大的差别,可粗略理解为TLS是SSL的继承者,但它们使用的是相同的SSLContext |
| | | SSLContext ctx = SSLContext.getInstance("TLS"); |
| | | // 使用TrustManager来初始化该上下文,TrustManager只是被SSL的Socket所使用 |
| | | // 使用TrustManager来初始化该上下文,TrustManager只是被SSL的Socket所使用 |
| | | ctx.init(null, new TrustManager[] { xtm }, null); |
| | | |
| | | /* |
| | |
| | | } |
| | | |
| | | /** |
| | | * 向HTTPS地址发送POST请求 |
| | | * 向HTTPS地址发送POST请求 |
| | | * |
| | | * @param reqURL |
| | | * 请求地址 |
| | | * 请求地址 |
| | | * @param params |
| | | * 请求参数 |
| | | * @return 响应内容 |
| | | * 请求参数 |
| | | * @return 响应内容 |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public String sendStringPostRequest(String reqURL, Map<String, String> params1) { |
| | |
| | | params = Collections.EMPTY_MAP; |
| | | } |
| | | |
| | | HttpEntity entity = sendPostRequest(reqURL, params); // 获取响应实体 |
| | | HttpEntity entity = sendPostRequest(reqURL, params); // 获取响应实体 |
| | | |
| | | if( null != entity ){ |
| | | String content = EntityUtils.toString(entity, UTF_8); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 向HTTPS地址发送POST请求 |
| | | * 向HTTPS地址发送POST请求 |
| | | * |
| | | * @param reqURL |
| | | * 请求地址 |
| | | * 请求地址 |
| | | * @param params |
| | | * 请求参数 |
| | | * @return 响应内容 |
| | | * 请求参数 |
| | | * @return 响应内容 |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public String sendStringPostRequest(String reqURL, String data) { |
| | |
| | | |
| | | StringEntity reqEntity = new StringEntity(data, UTF_8); |
| | | |
| | | HttpEntity entity = sendPostRequest(reqURL, Collections.EMPTY_MAP, reqEntity); // 获取响应实体 |
| | | HttpEntity entity = sendPostRequest(reqURL, Collections.EMPTY_MAP, reqEntity); // 获取响应实体 |
| | | |
| | | if( null != entity ){ |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 向HTTPS地址发送POST请求 |
| | | * 向HTTPS地址发送POST请求 |
| | | * |
| | | * @param reqURL |
| | | * 请求地址 |
| | | * 请求地址 |
| | | * @param params |
| | | * 请求参数 |
| | | * @return 响应内容 |
| | | * 请求参数 |
| | | * @return 响应内容 |
| | | */ |
| | | public String sendStringPostRequest(String reqURL, Map<String, String> headerParas1, String data) { |
| | | Map<String, String> headerParas = headerParas1; |
| | |
| | | |
| | | StringEntity reqEntity = new StringEntity(data, UTF_8); |
| | | |
| | | HttpEntity entity = sendPostRequest(reqURL, headerParas, reqEntity); // 获取响应实体 |
| | | HttpEntity entity = sendPostRequest(reqURL, headerParas, reqEntity); // 获取响应实体 |
| | | |
| | | if( null != entity ){ |
| | | String content = EntityUtils.toString(entity, UTF_8); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 向HTTPS地址发送POST请求 |
| | | * 向HTTPS地址发送POST请求 |
| | | * |
| | | * @param reqURL |
| | | * 请求地址 |
| | | * 请求地址 |
| | | * @param params |
| | | * 请求参数 |
| | | * @return 响应内容 |
| | | * 请求参数 |
| | | * @return 响应内容 |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public HttpEntity sendPostRequest(String reqURL, Map<String, String> params1) { |
| | |
| | | Map<String, String> headerParas = headerParas1; |
| | | try { |
| | | |
| | | HttpPost httpPost = new HttpPost(reqURL); // 创建HttpPost |
| | | HttpPost httpPost = new HttpPost(reqURL); // 创建HttpPost |
| | | httpPost.setEntity(reqEntity); |
| | | |
| | | headerParas = checkUserAgent(headerParas); |
| | |
| | | log.debug("request--header:" + header.getName() + ":" + header.getValue()); |
| | | } |
| | | |
| | | HttpResponse response = httpClient.execute(httpPost); // 执行POST请求 |
| | | HttpResponse response = httpClient.execute(httpPost); // 执行POST请求 |
| | | headers = response.getAllHeaders(); |
| | | for (Header header : headers) { |
| | | log.debug("response--header:" + header.getName() + ":" + header.getValue()); |
| | | } |
| | | HttpEntity entity = response.getEntity(); // 获取响应实体 |
| | | HttpEntity entity = response.getEntity(); // 获取响应实体 |
| | | |
| | | this.debug(httpPost.getURI(), response.getStatusLine().getStatusCode(), entity.getContentLength(), entity.getContentType()); |
| | | |
| | | if (302 == response.getStatusLine().getStatusCode() && response.containsHeader("location")) { |
| | | String location = response.getLastHeader("location").getValue(); |
| | | log.debug("重定向:" + location); |
| | | log.debug("重定向:" + location); |
| | | EntityUtils.consume(entity); |
| | | return sendPostRequest(location, headerParas, reqEntity); |
| | | } |
| | |
| | | Map<String, String> headerParas = headerParas1; |
| | | try { |
| | | |
| | | HttpDelete httpDelete = new HttpDelete(reqURL); // 创建HttpPost |
| | | HttpDelete httpDelete = new HttpDelete(reqURL); // 创建HttpPost |
| | | |
| | | headerParas = checkUserAgent(headerParas); |
| | | for (String key : headerParas.keySet()) { |
| | |
| | | log.debug("request--header:" + header.getName() + ":" + header.getValue()); |
| | | } |
| | | |
| | | HttpResponse response = httpClient.execute(httpDelete); // 执行POST请求 |
| | | HttpResponse response = httpClient.execute(httpDelete); // 执行POST请求 |
| | | headers = response.getAllHeaders(); |
| | | for (Header header : headers) { |
| | | log.debug("response--header:" + header.getName() + ":" + header.getValue()); |
| | | } |
| | | HttpEntity entity = response.getEntity(); // 获取响应实体 |
| | | HttpEntity entity = response.getEntity(); // 获取响应实体 |
| | | |
| | | this.debug(httpDelete.getURI(), response.getStatusLine().getStatusCode(), entity.getContentLength(), entity.getContentType()); |
| | | |
| | |
| | | * @param header |
| | | */ |
| | | private void debug(URI uri, int code, long length, Header header){ |
| | | log.debug("请求地址: " + uri); |
| | | log.debug("响应状态: " + code); |
| | | log.debug("响应长度: " + length); |
| | | log.debug("响应类型: " + header); |
| | | log.debug("请求地址: " + uri); |
| | | log.debug("响应状态: " + code); |
| | | log.debug("响应长度: " + length); |
| | | log.debug("响应类型: " + header); |
| | | } |
| | | |
| | | public String sendStringPutRequest(String reqURL, Map<String, String> headerParas1, String body) { |
| | | Map<String, String> headerParas = headerParas1; |
| | | try { |
| | | |
| | | HttpPut httpPut = new HttpPut(reqURL); // 创建HttpPost |
| | | HttpPut httpPut = new HttpPut(reqURL); // 创建HttpPost |
| | | |
| | | headerParas = checkUserAgent(headerParas); |
| | | for (String key : headerParas.keySet()) { |
| | |
| | | StringEntity reqEntity = new StringEntity(body, UTF_8); |
| | | httpPut.setEntity(reqEntity); |
| | | |
| | | HttpResponse response = httpClient.execute(httpPut); // 执行POST请求 |
| | | HttpResponse response = httpClient.execute(httpPut); // 执行POST请求 |
| | | headers = response.getAllHeaders(); |
| | | for (Header header : headers) { |
| | | log.debug("response--header:" + header.getName() + ":" + header.getValue()); |
| | | } |
| | | HttpEntity entity = response.getEntity(); // 获取响应实体 |
| | | HttpEntity entity = response.getEntity(); // 获取响应实体 |
| | | |
| | | this.debug(httpPut.getURI(), response.getStatusLine().getStatusCode(), entity.getContentLength(), entity.getContentType()); |
| | | |
| | |
| | | Map<String, String> headerParas = headerParas1; |
| | | try { |
| | | |
| | | HttpPost httpPost = new HttpPost(reqURL); // 创建HttpPost |
| | | HttpPost httpPost = new HttpPost(reqURL); // 创建HttpPost |
| | | |
| | | BasicHttpEntity requestBody = new BasicHttpEntity(); |
| | | requestBody.setContent(new ByteArrayInputStream(param.getBytes(UTF_8))); |
| | |
| | | |
| | | log.debug("request--param:" + param); |
| | | |
| | | HttpResponse response = httpClient.execute(httpPost); // 执行POST请求 |
| | | HttpResponse response = httpClient.execute(httpPost); // 执行POST请求 |
| | | headers = response.getAllHeaders(); |
| | | for (Header header : headers) { |
| | | log.debug("response--header:" + header.getName() + ":" + header.getValue()); |
| | | } |
| | | HttpEntity entity = response.getEntity(); // 获取响应实体 |
| | | HttpEntity entity = response.getEntity(); // 获取响应实体 |
| | | |
| | | this.debug(httpPost.getURI(), response.getStatusLine().getStatusCode(), entity.getContentLength(), entity.getContentType()); |
| | | |
| | | if (302 == response.getStatusLine().getStatusCode() && response.containsHeader("location")) { |
| | | String location = response.getLastHeader("location").getValue(); |
| | | log.debug("重定向:" + location); |
| | | log.debug("重定向:" + location); |
| | | EntityUtils.consume(entity); |
| | | return sendPostRequest(location, headerParas, param); |
| | | } |
| | |
| | | Map<String, String> params = params1; |
| | | Map<String, String> headerParas = headerParas1; |
| | | try { |
| | | // HttpGet httpGet = new HttpGet(reqURL); // 创建HttpPost |
| | | // HttpGet httpGet = new HttpGet(reqURL); // 创建HttpPost |
| | | |
| | | headerParas = checkUserAgent(headerParas); |
| | | |
| | |
| | | log.debug("request--header:" + header.getName() + ":" + header.getValue()); |
| | | } |
| | | |
| | | // HttpResponse response = httpClient.execute(httpGet); // 执行POST请求 |
| | | // HttpResponse response = httpClient.execute(httpGet); // 执行POST请求 |
| | | HttpResponse response = httpClient.execute(request); |
| | | headers = response.getAllHeaders(); |
| | | for (Header header : headers) { |
| | | log.debug("response--header:" + header.getName() + ":" + header.getValue()); |
| | | } |
| | | HttpEntity entity = response.getEntity(); // 获取响应实体 |
| | | HttpEntity entity = response.getEntity(); // 获取响应实体 |
| | | |
| | | this.debug(request.getURI(), response.getStatusLine().getStatusCode(), entity.getContentLength(), entity.getContentType()); |
| | | |
| | | if (302 == response.getStatusLine().getStatusCode() && response.containsHeader("location")) { |
| | | String location = response.getLastHeader("location").getValue(); |
| | | log.debug("重定向"); |
| | | log.debug("重定向"); |
| | | return sendGetRequestAsResponse(location, headerParas, params); |
| | | } |
| | | |