使用HttpsURLConnection发送POST请求
来源:互联网 发布:没有安卓 知乎 编辑:程序博客网 时间:2024/05/29 02:46
重写X509TrustManager
private static TrustManager myX509TrustManager = new X509TrustManager() { @Override public X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } };
static public String SendHttpsPOST(String url, List<NameValuePair> param, String data) { String result = null; //使用此工具可以将键值对编码成"Key=Value&Key2=Value2&Key3=Value3”形式的请求参数 String requestParam = URLEncodedUtils.format(param, "UTF-8"); try { //设置SSLContext SSLContext sslcontext = SSLContext.getInstance("TLS"); sslcontext.init(null, new TrustManager[]{myX509TrustManager}, null); //打开连接 //要发送的POST请求url?Key=Value&Key2=Value2&Key3=Value3的形式 URL requestUrl = new URL(url + "?" + requestParam); HttpsURLConnection httpsConn = (HttpsURLConnection)requestUrl.openConnection(); //设置套接工厂 httpsConn.setSSLSocketFactory(sslcontext.getSocketFactory()); //加入数据 httpsConn.setRequestMethod("POST"); httpsConn.setDoOutput(true); DataOutputStream out = new DataOutputStream( httpsConn.getOutputStream()); if (data != null) out.writeBytes(data); out.flush(); out.close(); //获取输入流 BufferedReader in = new BufferedReader(new InputStreamReader(httpsConn.getInputStream())); int code = httpsConn.getResponseCode(); if (HttpsURLConnection.HTTP_OK == code){ String temp = in.readLine(); /*连接成一个字符串*/ while (temp != null) { if (result != null) result += temp; else result = temp; temp = in.readLine(); } } } catch (KeyManagementException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; }
1 0
- 使用HttpsURLConnection发送POST请求
- 使用HttpsURLConnection发送POST请求
- 使用HttpsURLConnection发送POST请求
- 使用JDK自带HttpsURLConnection发送HTTPS请求
- java HttpsURLConnection发送https请求
- java HttpsURLConnection发送https请求
- 使用HttpURLConnection发送POST请求
- 使用C#发送POST请求
- 使用libcurl发送post请求
- 使用HttpClient发送POST请求
- 使用C#发送POST请求
- 使用RestTemplate发送post请求
- 使用httpclient发送post请求
- HttpsURLConnection实现SSL的GET/POST请求
- HttpsURLConnection实现SSL的GET/POST请求
- Java使用HttpsURLConnection进行Get和Post请求(绕过证书验证)
- Nodejs使用post发送请求与批量发送请求
- 使用HttpURLConnection发送post和get请求
- Android倒计时原理的实现
- Qt使用教程之创建Qt Quick应用程序(三)
- 航电1012 u Calculate e
- 98.set_include_path()
- Centos 7 最小化版本中无法使用ifconfig解析
- 使用HttpsURLConnection发送POST请求
- struts.xml-包配置
- 2016年数学建模美赛题目原文及翻译-B [个人思路]
- 家庭个人samba文件服务器搭建(一) 完全共享目录
- 终于搞定Mac下无线网卡安装不了的问题
- perl URLencode URLdecode的方法
- 微信红包算法
- Haproxy实战:80端口转发到webserver和ssh
- TCP三次握手/四次挥手 及 状态变迁图