记录一次Java中发起https请求数据接口
2020-08-20 20:32
197 查看
[code] import javax.net.ssl.*; import java.io.*; import java.net.URL; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.HashMap; import java.util.Map; public class HttpsUtil { private static final class DefaultTrustManager implements X509TrustManager { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return null; } } public static String getStringFromInputStream(InputStream inputStream) { StringBuffer str = new StringBuffer(); BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); try { String temp; while ((temp = br.readLine()) != null) { str.append(temp); } } catch (IOException e) { } finally { close(inputStream); } return str.toString(); } private static final void close(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (IOException e) { } } } public static Map<String, Object> doGet(String uri) { Map<String, Object> resultMap = new HashMap<>(); String msg = ""; int success = 0; try { SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom()); SSLSocketFactory ssf = ctx.getSocketFactory(); URL url = new URL(uri); HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection(); httpsConn.setSSLSocketFactory(ssf); httpsConn.setHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String arg0, SSLSession arg1) { return true; } }); httpsConn.setRequestMethod("GET"); httpsConn.setDoInput(true); httpsConn.setDoOutput(true); int respCode = httpsConn.getResponseCode(); if (respCode == 200) { success = 1; msg = getStringFromInputStream(httpsConn.getInputStream()); }else{ success = -1; msg = "response_status: " + respCode; } } catch (Exception e) { success = -2; msg = "error msg : " + e.getMessage(); e.printStackTrace(); } finally { } resultMap.put("msg", msg); resultMap.put("success", success); return resultMap; } public static Map<String, Object> doPost(String uri, String data) { Map<String, Object> resultMap = new HashMap<>(); String msg = ""; int success = 0; try { SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom()); SSLSocketFactory ssf = ctx.getSocketFactory(); URL url = new URL(uri); HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection(); httpsConn.setSSLSocketFactory(ssf); httpsConn.setHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String arg0, SSLSession arg1) { return true; } }); httpsConn.setRequestMethod("POST"); httpsConn.setDoInput(true); httpsConn.setDoOutput(true); // 参数写入 DataOutputStream out = new DataOutputStream(httpsConn.getOutputStream()); out.writeBytes(data); out.flush(); out.close(); int respCode = httpsConn.getResponseCode(); if (respCode == 200) { success = 1; msg = getStringFromInputStream(httpsConn.getInputStream()); }else{ success = -1; msg = "response_status: " + respCode; } } catch (Exception e) { success = -2; msg = "error msg : " + e.getMessage(); e.printStackTrace(); } finally { } resultMap.put("msg", msg); resultMap.put("success", success); return resultMap; } }
String httpsurl = ""; String pars = "key=1&secret=1&time=2&sign=3¶ms=333";// Map res = HttpsUtil.doPost(httpsurl,pars);
相关文章推荐
- VPOS MAS-CNP接口——建立https连接发送请求并接收返回的XML数据流
- Java 请求https接口不需要安装证书
- 如何在java中发起http和https请求
- java后台发起上传文件的post请求(http和https)
- 如何在java中发起http和https请求 配置信任
- Java防止重复数据多次调用接口导致数据库插入重复记录
- 如何在java中发起http和https请求
- JSON数据的处理(一)——后台访问外部接口,以JSON数据作为参数发起请求。
- nodejs发起HTTPS请求并获取数据
- 基于java 使用http-post方式请求https webservice接口
- Java服务器对外提供接口以及Android端向服务器请求数据
- 怎么解决重复提交的问题,即网络不好的时候,提交按钮点了一次,但是却发起了多次http url请求,数据库中写入了多条重复记录(当然ID是不重复的,因为毕竟是多条http url记录)
- java异步线程池同时请求多个接口数据
- java 发起HTTPS请求无证书报错问题
- java 发起HTTPS请求-SSL客户端
- java发http,https请求获取数据
- https_request请求接口返回数据
- java后台发起get请求获取响应数据
- 记录一次360浏览器兼容模式下定时刷新(请求接口)的缓存问题
- java 请求https接口 证书验证