android - java.io.IOException : SSL handshake failure: Failure in SSL library, 通常是协议(protocol)错误

标签 android url stream

我正在尝试使用来自服务器的给定链接使用 URL 类下载一些内容。

我要下载的代码是:

            URL url = new URL(downloadUrl);
            InputStream stream = url.openStream();
            byte[] content = new byte[stream.available()];
            stream.read(content);
            stream.close();

但是在运行时出现了以下异常:

 java.io.IOException: SSL handshake failure: Failure in SSL library, usually a protocol error
 error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:604 0xaf076228:0x00000000)
     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.nativeconnect(Native Method)
 ...

我使用的链接是这样的:

https://contentserver.com/d/761/34/215656/5de1a41ea3bc9c81978af95ed19b03286f64d9a3

我知道如果我在浏览器上输入它会下载一个文件,我想通过 Java 下载相同的文件。

谢谢

最佳答案

java从https url读取数据的代码

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.*;
import java.security.Security;
import java.util.Properties;

public class UseHttps {

public static void main(String argv[]) {

String fullURL = "https://fortress.wa.gov/lni/bbip/detail.aspx?License=SIBLUCL004C5";
try {

    URL page = new URL(fullURL); // Process the URL far enough to find the right handler
    URLConnection urlc = page.openConnection();
    urlc.setUseCaches(false); // Don't look at possibly cached data
    System.out.println("Content-type = " + urlc.getContentType()); // See what's here
    System.out.println("Content-length = " + urlc.getContentLength()); // See how much of it there is
    // Read it all and print it out
    BufferedReader br = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
    String buffer = "";
    while (buffer != null) {
        try {
            System.out.println(buffer);
            buffer = br.readLine();
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
            break;
        }
    }
}
catch (MalformedURLException mue) {
System.out.println(fullURL + " is not a URL that can be resolved");
}
catch (IOException ie) {
ie.printStackTrace();
}
}
}

关于android - java.io.IOException : SSL handshake failure: Failure in SSL library, 通常是协议(protocol)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5250502/

相关文章:

java - 程序在 Java 中初始化对象输入流时暂停

android - AlertDialog setOnDismissListener 不工作

url - Heroku 自定义 url 用作重定向而不是别名

android - picasso 从 URL 下载多张图片

c# - 什么是用于 c# 和 .net 的良好商业 tar 流库?

c# - 在使用中包装 MemoryStream

android - 如何通过向右拖动主页来停止滑动

android - 使用 backstack 时 fragment 未替换

android - 自定义 Navigationview 菜单 UI Android

javascript - 根据当前日期获取 XML 文件