Java请求https方法
来源:互联网 发布:广电网络智慧社区 编辑:程序博客网 时间:2024/06/16 09:50
参考百度链接:https://zhidao.baidu.com/question/1694899703762628988.html
时间:2017-08-30
SSLClient类,继承至HttpClient
import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.message.BasicHeader;import org.apache.http.util.EntityUtils;import org.json.JSONObject;public class SSLClient extends DefaultHttpClient{ public SSLClient() throws Exception { super(); SSLContext ctx = SSLContext.getInstance("TLS"); X509TrustManager tm = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] chain,String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return null; } }; ctx.init(null, new TrustManager[]{tm}, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); ClientConnectionManager ccm = this.getConnectionManager(); SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", 443, ssf)); } }
HttpClient发送post请求的类
import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient;public class HttpClientUtil { public String doPost(String url,JSONObject json,String charset) { /* * 利用HttpClient进行post请求的工具类 */ HttpClient httpClient = null; HttpPost httpPost = null; String result = null; try { httpClient = new SSLClient(); httpPost = new HttpPost(url); StringEntity se = new StringEntity(json.toString(),"UTF-8"); se.setContentType("application/json"); se.setContentEncoding(new BasicHeader("text/json", "application/json")); httpPost.setEntity(se); HttpResponse response = httpClient.execute(httpPost); if(response != null) { HttpEntity resEntity = response.getEntity(); if(resEntity != null) { result = EntityUtils.toString(resEntity,charset); } } }catch(Exception ex) { ex.printStackTrace(); } return result; } }
测试类
import org.json.JSONArray;import org.json.JSONObject;public class Test extends Thread{ public static void main ( String args[] ) { //test(); } //维修企业账户注册 public static void test() { String method = "https://zjtest.qichedangan.cn/restservices/lcipprodatarest/lcipproaccountcompany/query"; try { JSONObject json1 = new JSONObject(); json1.put("companyname","测试2017082902"); json1.put("companypassword","88888888a"); HttpClientUtil httpClientUtil = new HttpClientUtil(); String httpOrgCreateTestRtn = httpClientUtil.doPost(method,json1,"utf-8"); if(httpOrgCreateTestRtn != null) { System.out.println("返回结果: " +httpOrgCreateTestRtn); JSONObject json = new JSONObject(httpOrgCreateTestRtn); if(!json.isNull("code")) { String code = json.getString("code"); String status = json.getString("status"); if(code.equals("1")) { String companycode = json.getString("companycode"); System.out.println("维修企业编码:" + companycode); }else { System.out.println("未通过原因:" + status); } } } } catch (Exception e) { e.printStackTrace(); } }
结果示例
返回结果: {"status":"新增成功","code":"1"}
阅读全文
1 0
- Java请求https方法
- Java 发送https 的post请求方法
- https请求方法总结
- Java https请求 HttpsURLConnection
- java 实现https请求
- java 实现https请求
- java Https的请求
- java 发送 https请求
- java 实现https请求
- Java https请求 HttpsURLConnection
- JAVA请求HTTPS协议
- java访问https请求
- java发送https请求
- JAVA请求HTTPS协议
- java 实现https请求
- java模拟https请求
- java模拟https请求
- java 实现https请求
- Java 易遗漏知识点
- vs 2015 Commmunity 每次启动都提示Key Required解决方案
- 【Java】边框总结
- 如何上手深度学习中的图像处理?有这个代码资源库就够了
- 啊发生的
- Java请求https方法
- Java设计模式--单例模式
- opencv 网站tutorials学习翻译
- 【Jfinal】分页 Ajax点击加载
- C++ 图论-最短路径问题
- iOS转前端之JS进阶技巧及Demo演示
- 卷积神经网络CNN-目标探测
- XZ_JavaScript之JavaScript的基本语法
- Google正面迎战苹果,用ARCore阻击ARKit