如果您的CA不受系统信任,如何将Android应用程序连接到SSL服务器?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如果您的CA不受系统信任,如何将Android应用程序连接到SSL服务器?相关的知识,希望对你有一定的参考价值。

我正在尝试从使用SSL保护的网站下载图像。当我在API 23(android 6.0)或更高版本上运行App时,下载图像是成功的,但是当App在较低的API上运行时,我得到SSLHandshakeException,其中包含“java.security.cert.CertPathValidator异常:找不到证书路径的信任锚。 “ 信息。

答案

解决方案就在指尖之下。试试这个链接Security with HTTPS and SSL。这里提供完整的解决方案

另一答案

您还可以使用DownloadManager从Url下载图像并保存到SD卡

DownloadImages(PictureURL,ImageSavePath);

public void DownloadImages(String theUrl, String thePath) {
        Uri Download_Uri = Uri.parse(theUrl);
        DownloadManager downloadManager = (DownloadManager) myContext.getSystemService(myContext.DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(Download_Uri);

        //Restrict the types of networks over which this download may proceed.
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
        //Set whether this download may proceed over a roaming connection.
        request.setAllowedOverRoaming(true);
        //Set the local destination for the downloaded file to a path within the application's external files directory
        String[] split = theUrl.split("/");
        //request.setDestinationInExternalFilesDir(myContext, mySdCardImagePath, split[split.length - 1]);
        //request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, split[split.length-1]);

        File destinationFile = new File(thePath, split[split.length - 1]);
        Uri uri = null;

        // naugat does not need provider here
        uri = Uri.fromFile(destinationFile);

        request.setDestinationUri(uri);

        //Set the title of this download, to be displayed in notifications (if enabled).
        request.setTitle(split[split.length - 1]);
        //Set a description of this download, to be displayed in notifications (if enabled)
        request.setDescription(thePath);

        request.setVisibleInDownloadsUi(true);

        //Enqueue a new download and get the reference Id
        long downloadReference = downloadManager.enqueue(request);
        //Check_Image_Status(downloadReference);
    }
另一答案
// Glide Image loader
compile 'com.github.bumptech.glide:glide:3.7.0'
Use following code inside onBindViewHolder
    int defaultImagePath = R.drawable.default_thumb;
    int errorImagePath = R.drawable.damaged_image;
    holder.mImageViewContactItem.setImageResource(defaultImagePath);

    String uri = photoPath;
    loadImageWithGlide(context, holder.mImageViewContactItem, uri, defaultImagePath,
                            errorImagePath);


public  void loadImageWithGlide(final Context context, ImageView theImageViewToLoadImage,
                                          String theLoadImagePath, int theDefaultImagePath, int tehErrorImagePath) {
        if (context == null) return;

        Glide.with(context) //passing context
                .load(theLoadImagePath) //passing your url to load image.
                .placeholder(theDefaultImagePath) //this would be your default image (like default profile or logo etc). it would be loaded at initial time and it will replace with your loaded image once glide successfully load image using url.
                .error(tehErrorImagePath)//in case of any glide exception or not able to download then this image will be appear . if you won't mention this error() then nothing to worry placeHolder image would be remain as it is.
                .diskCacheStrategy(DiskCacheStrategy.ALL) //using to load into cache then second time it will load fast.
                //.animate(R.anim.fade_in) // when image (url) will be loaded by glide then this face in animation help to replace url image in the place of placeHolder (default) image.
                //.fitCenter()//this method help to fit image into center of your ImageView
                .into(theImageViewToLoadImage); //pass imageView reference to appear the image.

        /*  Normal way to Load Image with Glide.
                Glide.with(theContext)
                .load(theImagePath)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(theImageView);*/
    }

以上是关于如果您的CA不受系统信任,如何将Android应用程序连接到SSL服务器?的主要内容,如果未能解决你的问题,请参考以下文章

如何将浏览器中不受信任的证书手动设置为信任

如何阻止不受信任的证书?

如何在移动设备上安装android studio创建的应用程序而无需将其连接到系统

如何在 Android 设备上安装受信任的 CA 证书?

https一定是安全的吗

charles进行手机抓包:安全证书不受信任,错误码3 怎么解决下?