热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

androidvolleyhttps

2019独角兽企业重金招聘Python工程师标准Android中使用volley进行Https通讯的时候,如果没有申请正式会报错:(我们的服务器用

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

Android中使用volley进行Https 通讯的时候,如果没有申请正式会报错:( 我们的服务器用nginx作为容器 )

VolleyEror: com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

最好的办法是按照规则来办事:加证书。然而调试服务器说不加...

那么要怎么才不会报错呢?

1.查看接口 X509TrustManger.java ( 在包javax.net.ssl )

X509TrustManager.Java
//------------------------------------package javax.net.ssl;import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;/*** The trust manager for X509 certificates to be used to perform authentication* for secure sockets.*/
public interface X509TrustManager extends TrustManager {/*** Checks whether the specified certificate chain (partial or complete) can* be validated and is trusted for client authentication for the specified* authentication type.** @param chain*            the certificate chain to validate.* @param authType*            the authentication type used.* @throws CertificateException*             if the certificate chain can't be validated or isn't trusted.* @throws IllegalArgumentException*             if the specified certificate chain is empty or {@code null},*             or if the specified authentication type is {@code null} or an*             empty string.*/public void checkClientTrusted(X509Certificate[] chain, String authType)throws CertificateException;/*** Checks whether the specified certificate chain (partial or complete) can* be validated and is trusted for server authentication for the specified* key exchange algorithm.** @param chain*            the certificate chain to validate.* @param authType*            the key exchange algorithm name.* @throws CertificateException*             if the certificate chain can't be validated or isn't trusted.* @throws IllegalArgumentException*             if the specified certificate chain is empty or {@code null},*             or if the specified authentication type is {@code null} or an*             empty string.*/public void checkServerTrusted(X509Certificate[] chain, String authType)throws CertificateException;/*** Returns the list of certificate issuer authorities which are trusted for* authentication of peers.** @return the list of certificate issuer authorities which are trusted for*         authentication of peers.*/public X509Certificate[] getAcceptedIssuers();
}//-------------------------------------------------------------------------------


2.FakeX509TrustManger  implements X509TrustManager

package com.http.utils;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

/**
*
*
* Created by Administrator on 2016/2/17.
*/
public class FakeX509TrustManager implements X509TrustManager {

   private static TrustManager[] trustManagers;
   private static final X509Certificate[] _AcceptedIssuers = new
           X509Certificate[] {};

   @Override
   public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws java.security.cert.CertificateException {
       //To change body of implemented methods use File | Settings | File Templates.
   }

   @Override
   public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws java.security.cert.CertificateException {
       //To change body of implemented methods use File | Settings | File Templates.
   }

   public boolean isClientTrusted(X509Certificate[] chain) {
       return true;
   }

   public boolean isServerTrusted(X509Certificate[] chain) {
       return true;
   }

   @Override
   public X509Certificate[] getAcceptedIssuers() {
       return _AcceptedIssuers;
   }

   public static void allowAllSSL() {
       HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

           @Override
           public boolean verify(String arg0, SSLSession arg1) {
               // TODO Auto-generated method stub
               return true;
           }

       });

       SSLContext context = null;
       if (trustManagers == null) {
           trustManagers = new TrustManager[] { new FakeX509TrustManager() };
       }

       try {
           context = SSLContext.getInstance("TLS");
           context.init(null, trustManagers, new SecureRandom());
       } catch (NoSuchAlgorithmException e) {
           e.printStackTrace();
       } catch (KeyManagementException e) {
           e.printStackTrace();
       }

       HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
   }

}


3.在请求前设置忽略所有的验证,允许所有的SSL


FakeX509TrustManager.allowAllSSL(); //it is dangerous!但是有的时候我们需要这样做!!//========================StringRequest=====================================================StringRequest httpRequest = new StringRequest(requestMethod, url, new Response.Listener() {@Overridepublic void onResponse(String response) {。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。


参考链接:http://www.trinea.cn/android/android-java-https-ssl-exception-2/


转:https://my.oschina.net/zengliubao/blog/616666



推荐阅读
  • Servlet多用户登录时HttpSession会话信息覆盖问题的解决方案
    本文讨论了在Servlet多用户登录时可能出现的HttpSession会话信息覆盖问题,并提供了解决方案。通过分析JSESSIONID的作用机制和编码方式,我们可以得出每个HttpSession对象都是通过客户端发送的唯一JSESSIONID来识别的,因此无需担心会话信息被覆盖的问题。需要注意的是,本文讨论的是多个客户端级别上的多用户登录,而非同一个浏览器级别上的多用户登录。 ... [详细]
  • 解决nginx启动报错epoll_wait() reported that client prematurely closed connection的方法
    本文介绍了解决nginx启动报错epoll_wait() reported that client prematurely closed connection的方法,包括检查location配置是否正确、pass_proxy是否需要加“/”等。同时,还介绍了修改nginx的error.log日志级别为debug,以便查看详细日志信息。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • Jboss的EJB部署描述符standardjaws.xml配置步骤详解
    本文详细介绍了Jboss的EJB部署描述符standardjaws.xml的配置步骤,包括映射CMP实体EJB、数据源连接池的获取以及数据库配置等内容。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 如何用UE4制作2D游戏文档——计算篇
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了如何用UE4制作2D游戏文档——计算篇相关的知识,希望对你有一定的参考价值。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • 本文介绍了RPC框架Thrift的安装环境变量配置与第一个实例,讲解了RPC的概念以及如何解决跨语言、c++客户端、web服务端、远程调用等需求。Thrift开发方便上手快,性能和稳定性也不错,适合初学者学习和使用。 ... [详细]
  • 本文介绍了计算机网络的定义和通信流程,包括客户端编译文件、二进制转换、三层路由设备等。同时,还介绍了计算机网络中常用的关键词,如MAC地址和IP地址。 ... [详细]
  • 在重复造轮子的情况下用ProxyServlet反向代理来减少工作量
    像不少公司内部不同团队都会自己研发自己工具产品,当各个产品逐渐成熟,到达了一定的发展瓶颈,同时每个产品都有着自己的入口,用户 ... [详细]
  • 本文介绍了Java的集合及其实现类,包括数据结构、抽象类和具体实现类的关系,详细介绍了List接口及其实现类ArrayList的基本操作和特点。文章通过提供相关参考文档和链接,帮助读者更好地理解和使用Java的集合类。 ... [详细]
  • Oracle优化新常态的五大禁止及其性能隐患
    本文介绍了Oracle优化新常态中的五大禁止措施,包括禁止外键、禁止视图、禁止触发器、禁止存储过程和禁止JOB,并分析了这些禁止措施可能带来的性能隐患。文章还讨论了这些禁止措施在C/S架构和B/S架构中的不同应用情况,并提出了解决方案。 ... [详细]
  • svnWebUI:一款现代化的svn服务端管理软件
    svnWebUI是一款图形化管理服务端Subversion的配置工具,适用于非程序员使用。它解决了svn用户和权限配置繁琐且不便的问题,提供了现代化的web界面,让svn服务端管理变得轻松。演示地址:http://svn.nginxwebui.cn:6060。 ... [详细]
  • Spring框架《一》简介
    Spring框架《一》1.Spring概述1.1简介1.2Spring模板二、IOC容器和Bean1.IOC和DI简介2.三种通过类型获取bean3.给bean的属性赋值3.1依赖 ... [详细]
  • Nginx Buffer 机制引发的下载故障
    Nginx ... [详细]
author-avatar
W布二
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有