使用HttpsURLConnection发送POST请求
来源:互联网 发布:手机运动软件 编辑:程序博客网 时间:2024/05/29 03:30
重写X509TrustManager
1 private static TrustManager myX509TrustManager = new X509TrustManager() { 2 3 @Override 4 public X509Certificate[] getAcceptedIssuers() { 5 return null; 6 } 7 8 @Override 9 public void checkServerTrusted(X509Certificate[] chain, String authType) 10 throws CertificateException { 11 } 12 13 @Override 14 public void checkClientTrusted(X509Certificate[] chain, String authType) 15 throws CertificateException { 16 } 17 };
1 static public String SendHttpsPOST(String url, List<NameValuePair> param, String data) 2 { 3 String result = null; 4 5 6 //使用此工具可以将键值对编码成"Key=Value&Key2=Value2&Key3=Value3”形式的请求参数 7 String requestParam = URLEncodedUtils.format(param, "UTF-8"); 8 try { 9 //设置SSLContext 10 SSLContext sslcontext = SSLContext.getInstance("TLS"); 11 sslcontext.init(null, new TrustManager[]{myX509TrustManager}, null);12 13 //打开连接14 //要发送的POST请求url?Key=Value&Key2=Value2&Key3=Value3的形式 15 URL requestUrl = new URL(url + "?" + requestParam); 16 HttpsURLConnection httpsConn = (HttpsURLConnection)requestUrl.openConnection();17 18 //设置套接工厂 19 httpsConn.setSSLSocketFactory(sslcontext.getSocketFactory());20 21 //加入数据 22 httpsConn.setRequestMethod("POST"); 23 httpsConn.setDoOutput(true); 24 DataOutputStream out = new DataOutputStream( 25 httpsConn.getOutputStream()); 26 if (data != null) 27 out.writeBytes(data); 28 29 out.flush(); 30 out.close();31 32 //获取输入流 33 BufferedReader in = new BufferedReader(new InputStreamReader(httpsConn.getInputStream())); 34 int code = httpsConn.getResponseCode(); 35 if (HttpsURLConnection.HTTP_OK == code){ 36 String temp = in.readLine(); 37 /*连接成一个字符串*/ 38 while (temp != null) { 39 if (result != null) 40 result += temp; 41 else 42 result = temp; 43 temp = in.readLine(); 44 } 45 } 46 } catch (KeyManagementException e) { 47 e.printStackTrace(); 48 } catch (NoSuchAlgorithmException e) { 49 e.printStackTrace(); 50 } catch (MalformedURLException e) { 51 e.printStackTrace(); 52 } catch (ProtocolException e) { 53 e.printStackTrace(); 54 } catch (IOException e) { 55 e.printStackTrace(); 56 }57 58 return result; 59 }
0 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请求
- Cordova 插件开发(plugin)
- 验证密码复杂度
- 第七周 课后实践:项目二——自选图形用户界面程序开发(求三角形面积)
- yii2学习笔记(三)Composer代理
- 【jQuery基础编程】jQuery-UI实现折叠面板里的控件拖曳
- 使用HttpsURLConnection发送POST请求
- php 之管理后台登陆超时 点击左侧菜单 跳转出整个frameset 显示登录 (smallnews笔记)
- 浮点数在计算机中存储方式(转)
- 让Office(word excel)显示在网页中的实现技术
- uva 10391 compound words
- 关于sql server添加sql认证===
- uva 514
- 什么是事件起泡,用jquery阻止事件起泡
- 【MSAR】当机器学习遇到计算机视觉——下篇