| | |
| | | import org.apache.http.message.BasicNameValuePair; |
| | | import org.apache.http.ssl.SSLContextBuilder; |
| | | import org.apache.http.util.EntityUtils; |
| | | import sun.misc.BASE64Encoder; |
| | | |
| | | import javax.net.ssl.SSLContext; |
| | | |
| | |
| | | |
| | | HttpGet get = new HttpGet(builder.build()); |
| | | |
| | | |
| | | response = httpclient.execute(get); |
| | | |
| | | System.out.println(response.getStatusLine()); |
| | | |
| | | if (200 == response.getStatusLine().getStatusCode()) { |
| | | HttpEntity entity = response.getEntity(); |
| | | System.out.println(entity.toString()); |
| | | resultString = EntityUtils.toString(entity, "utf-8"); |
| | | System.out.println(resultString.toString()); |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | |
| | | return resultString; |
| | | } |
| | | |
| | | /** |
| | | * 执行有参GET请求,带请求头 |
| | | * |
| | | * @param url 请求url |
| | | * @param params 参数 |
| | | * @param key 请求头Key |
| | | * @param secretKey 秘钥 |
| | | * @return |
| | | */ |
| | | public static String doGetHeader(String url, String key, String secretKey,Map<String, String> params) { |
| | | |
| | | //获取httpclient客户端 |
| | | CloseableHttpClient httpclient = HttpClients.createDefault(); |
| | | |
| | | String resultString = ""; |
| | | |
| | | CloseableHttpResponse response = null; |
| | | |
| | | try { |
| | | URIBuilder builder = new URIBuilder(url); |
| | | |
| | | if (null != params) { |
| | | for (String keys : params.keySet()) { |
| | | builder.addParameter(keys,params.get(keys)); |
| | | //builder.setParameter(keys, params.get(keys)); |
| | | } |
| | | } |
| | | |
| | | HttpGet httpGet = new HttpGet(builder.build()); |
| | | |
| | | //设置请求头 |
| | | httpGet.addHeader(key,secretKey); |
| | | // 传输的类型 |
| | | httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded"); |
| | | //执行Http请求调用 |
| | | response = httpclient.execute(httpGet); |
| | | //判断是否请求成功返回 |
| | | if (200 == response.getStatusLine().getStatusCode()) { |
| | | HttpEntity entity = response.getEntity(); |
| | | resultString = EntityUtils.toString(entity, "utf-8"); |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | if (null != response) { |
| | | try { |
| | | response.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | if (null != httpclient) { |
| | | try { |
| | | httpclient.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | return resultString; |
| | | } |
| | | |
| | | /** |
| | | * 执行有参GET请求,带请求头,接收图片流 |
| | | * |
| | | * @param url 请求url |
| | | * @param params 参数 |
| | | * @param key 请求头Key |
| | | * @param secretKey 秘钥 |
| | | * @return |
| | | */ |
| | | public static String doGetHeaderPictureBase64(String url, String key, String secretKey,Map<String, String> params) { |
| | | |
| | | //获取httpclient客户端 |
| | | CloseableHttpClient httpclient = HttpClients.createDefault(); |
| | | String resultString = ""; |
| | | CloseableHttpResponse response = null; |
| | | try { |
| | | URIBuilder builder = new URIBuilder(url); |
| | | if (null != params) { |
| | | for (String keys : params.keySet()) { |
| | | builder.addParameter(keys,params.get(keys)); |
| | | } |
| | | } |
| | | HttpGet httpGet = new HttpGet(builder.build()); |
| | | //设置请求头 |
| | | httpGet.addHeader(key,secretKey); |
| | | // 传输的类型 |
| | | httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded"); |
| | | //执行Http请求调用 |
| | | response = httpclient.execute(httpGet); |
| | | // 将返回的图片或者文件转化成字节数组的形式 |
| | | byte[] data = EntityUtils.toByteArray(response.getEntity()); |
| | | BASE64Encoder encoder = new BASE64Encoder(); |
| | | //String imageBase64 = "data:image/png;base64," + encoder.encodeBuffer(data).trim(); |
| | | return encoder.encodeBuffer(data).trim().replaceAll("\n", "").replaceAll("\r", "").replaceAll(" ", "");//删除 \r\n |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | if (null != response) { |
| | | try { |
| | | response.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | if (null != httpclient) { |
| | | try { |
| | | httpclient.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | return resultString; |
| | | } |
| | | |
| | | /** |
| | | * 执行无参GET请求 |
| | | * |