使用 Https 调用 https 接口
来源:互联网 发布:手机视频聊天软件 编辑:程序博客网 时间:2024/06/05 20:37
最近公司让用 Java 调用一个国外的接口,打开文档一看 要使用 https 调用 https 接口。弄了几天终于弄的差不多了。
首先配置本地单向 https
1.生成安全证书(个人学习可以使用工具生成,而如果是公司的则另当别论了)
这个网上一大堆教程
2.配置tomcat 服务器
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" keystoreFile="F:\tomcat.keystore" keystorePass="tomcat" ciphers="tomcat" />
手动复制,端口号可修改
3.修改 web.xml 配置文件
<security-constraint> <!-- Authorization setting for SSL --> <web-resource-collection > <web-resource-name >SSL</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint>
上面的是针对所有请求转化的,有具体需要自行百度
完成上面的部分以后就可以使用 https 访问你自己的 action , jsp 什么的了。
调用 https 第三方接口
- 配置 SSLClient ,让它继承 DefaultHttpClient,具体代码如下:
public SSLClient() throws Exception{ super(); SSLContext ctx = SSLContext.getInstance("TLS"); X509TrustManager tm = new 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; } }; 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)); }
然后我们就可以愉快的使用 https 了,举一个例子,我们使用 httpClient 进行文件和其它数据的上传
//传递图片到 April 官网上 左边为文件名 ,后边为文件路径 public static String upPicToApril(String filename , String file ){ String url = "******";//url //1:创建一个httpclient对象 HttpClient httpclient = null; HttpPost httppost = null ; Charset charset = Charset.forName("UTF-8");//设置编码 String author = "Basic " + Base64.encode(("username:password").getBytes()); String resString = null ; try { try { httpclient = new SSLClient(); httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,600000); httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,900000); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //2:创建http的发送方式对象,是GET还是post httppost = new HttpPost(url); httppost.setHeader("Authorization", author); httppost.setHeader("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;)"); //3:创建要发送的实体,就是key-value的这种结构,借助于这个类,可以实现文件和参数同时上传,很简单的。 MultipartEntity reqEntity = new MultipartEntity(); //图片位置 FileBody bin = new FileBody(new File(file)); StringBody comment = new StringBody(filename,charset); reqEntity.addPart("image", bin); reqEntity.addPart("filename", comment); httppost.setEntity(reqEntity); //4:执行httppost对象,从而获得信息 HttpResponse response1 = httpclient.execute(httppost); HttpEntity resEntity = response1.getEntity(); //获得返回来的信息,转化为字符串string resString = EntityUtils.toString(resEntity); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {} } return resString; }
完工。。。。。。
阅读全文
0 0
- 使用 Https 调用 https 接口
- Android 使用Hprose 调用Https接口
- http/https接口调用
- delphi调用https接口
- 基于java调用https接口
- 基于java调用https接口
- webservice:客户端调用https接口
- android中使用Retrofit调用https接口及缓存
- android中使用Retrofit调用https接口及缓存
- webservice 绕过证书验证调用https接口
- java调用https接口报错
- JAVA利用HttpClient进行HTTPS接口调用
- 调用https接口的工具类
- JAVA利用HttpClient进行HTTPS接口调用
- JAVA利用HttpClient进行HTTPS接口调用
- java代码使用https访问接口
- 【JAVA WEB】使用HTTPS请求外部接口
- 使用Https
- Android 多媒体(播放应用内的音乐)
- Erlang并发编程基础(学习总结)
- HDU 2601 An easy problem
- Linux系统IO分析工具之iotop参数详解(查看IO占用)
- hdu1671-Phone List 字典树
- 使用 Https 调用 https 接口
- 【solr】Solr 4.10.3搭建
- android-Service和Thread的区别
- zigbee协议栈 中断服务函数定义方法
- 对象和类
- 浏览器加载和渲染html的顺序
- 7.18
- Parentheses Balance
- 嵌入式学习日记(十二)