httpcomponents for https without CE
来源:互联网 发布:led banner软件下载 编辑:程序博客网 时间:2024/06/05 14:56
private static class DefaultTrustManager implements X509TrustManager{ // concept from http://hc.apache.org/index.html @Override public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { // do nothing here } @Override public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { // do nothing here } @Override public X509Certificate[] getAcceptedIssuers() { return null; }}
Then, initialize the custom trust manager, and allow any SSL connection, regardless of its validity. We are in control of the connection here and so we don’t have anything to hide:
import java.io.*;import import org.apache.http.*;import javax.net.ssl.*;import java.security.cert*;public void SSLImgPost(String imgurl, String fileloc) throws Exception { if (fileloc.length() < 1) { System.out.println("File path not given"); } TrustManager[] trustAllCerts = new TrustManager[] { new DefaultTrustManager() }; SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, trustAllCerts, new java.security.SecureRandom());
Then, tell the SSL socket factory to nuder the SSL certificate hostname verifier :
SSLSocketFactory sf = new SSLSocketFactory( sc, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); Scheme sch = new Scheme("https", 443, sf); httpclient = new DefaultHttpClient(); // httpclient is a static reference httpclient.getConnectionManager().getSchemeRegistry().register(sch);
Then, follow through with reading in the file and sending it to the server with a POST:
HttpPost httppost = new HttpPost(imgurl); File file = new File(fileloc); InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1); try { reqEntity.setContentType("binary/octet-stream"); reqEntity.setChunked(true); httppost.setEntity(reqEntity); System.out.println("Executing post IMG upload " + httppost.getRequestLine()); HttpResponse response = httpclient.execute(httppost); HttpEntity resEntity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (resEntity != null) { System.out.println("Response content length: " + resEntity.getContentLength()); System.out.println("Chunked?: " + resEntity.isChunked()); } EntityUtils.consume(resEntity); } finally { httpclient.getConnectionManager().shutdown(); }}
- httpcomponents for https without CE
- httpcomponents创建HTTPS调用
- httpcomponents https请求实例
- httpcomponents-client-4.3.4 HTTPS地址访问
- HttpComponents
- Connecting through Http or Https for CE/Windows Mobile c++/vc++方法一
- Windows CE for >
- Gitignore for Magento CE
- 使用apache的httpcomponents中的httpclient 4.5对https进行访问
- Perl 5.6 for Windows CE
- "sysgen_" dependency for Windows CE
- How to: Connect to Windows CE Device Without ActiveSync
- How to: Connect to Windows CE Device Without ActiveSync
- How to: Connect to Windows CE Device Without ActiveSync
- httpcomponents练习
- HttpComponents实战
- HttpComponents概述
- Apache HttpComponents
- 编程珠玑 习题 十二章
- dedecms升级到V5.7后台按钮,编辑器无法显示
- 2012第12周国内Android应用下载排行榜
- MyBatis之作用域和生命周期(二)
- 关于C语言的指针传递地址的一些思考
- httpcomponents for https without CE
- Android之Sensor 简介
- 使用Viewfliper实现仿launcher横向滑动风格
- Android PromptDialog example
- U-boot源代码全分析系列(基于PowerPC)
- java基础加强视频zxx(33-41)
- Jquery 回车提交表单
- ASP.NET中用图片的方式实现静态页面累加访问量
- 一个http处理工具