相关疑难解决方法(0)

如何使用Apache HttpClient处理无效的SSL证书?

我知道,关于这个问题有很多不同的问题和很多答案...但我无法理解......

我有:ubuntu-9.10-desktop-amd64 + NetBeans6.7.1从"关闭"安装.代表.我需要通过HTTPS连接到某个站点.为此我使用Apache的HttpClient.

从教程我读到:

"一旦正确安装了JSSE,通过SSL进行安全的HTTP通信
就像普通的HTTP通信一样简单." 还有一些例子:

HttpClient httpclient = new HttpClient();
GetMethod httpget = new GetMethod("https://www.verisign.com/"); 
try { 
  httpclient.executeMethod(httpget);
  System.out.println(httpget.getStatusLine());
} finally {
  httpget.releaseConnection();
}
Run Code Online (Sandbox Code Playgroud)

到现在为止,我写道:

HttpClient client = new HttpClient();

HttpMethod get = new GetMethod("https://mms.nw.ru");
//get.setDoAuthentication(true);

try {
    int status = client.executeMethod(get);
    System.out.println(status);

    BufferedInputStream is = new BufferedInputStream(get.getResponseBodyAsStream());
    int r=0;byte[] buf = new byte[10];
    while((r = is.read(buf)) > 0) {
        System.out.write(buf,0,r);
    }

} catch(Exception ex) {
    ex.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

结果我有一组错误:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable …
Run Code Online (Sandbox Code Playgroud)

java ssl https apache-commons-httpclient

120
推荐指数
8
解决办法
29万
查看次数

116
推荐指数
13
解决办法
22万
查看次数

忽略Apache HttpClient 4.3中的SSL证书

如何忽略Apache HttpClient 4.3的 SSL证书(全部信任)?

我在SO上找到的所有答案都会处理以前的版本,并且API已更改.

有关:

编辑:

  • 它仅用于测试目的.孩子们,不要在家里(或在生产中)尝试

java ssl apache-httpcomponents

97
推荐指数
8
解决办法
16万
查看次数

使用Java忽略SSL证书错误

Apache Http客户端.您可以在此处查看相关代码:

String url = "https://path/to/url/service";
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);

// Test whether to ignore cert errors
if (ignoreCertErrors){
  TrustManager[] trustAllCerts = new TrustManager[]{
    new X509TrustManager(){
      public X509Certificate[] getAcceptedIssuers(){ return null; }
      public void checkClientTrusted(X509Certificate[] certs, String authType) {}
      public void checkServerTrusted(X509Certificate[] certs, String authType) {}
    }
  };

  try {
    SSLContext sslContext = SSLContext.getInstance("SSL");
    sslContext.init(null, trustAllCerts, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
  } catch (Exception e){
    e.printStackTrace();
  }
}

try {

  // Execute the method …
Run Code Online (Sandbox Code Playgroud)

java ssl ssl-certificate java-ee

9
推荐指数
1
解决办法
8万
查看次数

可以在没有证书验证的情况下进行httpS连接的kotlin库(如curl --insecure)

我需要抓取已过期/自签名证书的公司内部网站.没有人会为该主机配置有效的证书,所以我必须使用不安全的连接.

curl--insecure为此目的有旗帜,

Scala finagle库有.tlsWithoutValidation()模式.

问题:Kotlin库有类似的选择吗?

UPD:到目前为止,我正在使用Fuel这里发现的大量解决方法,但仍在寻找更好的方法..

fun useInsecureSSL() {

    // Create a trust manager that does not validate certificate chains
    val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager {
        override fun getAcceptedIssuers(): Array<X509Certificate>? = null
        override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) = Unit
        override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) = Unit
    })

    val sc = SSLContext.getInstance("SSL")
    sc.init(null, trustAllCerts, java.security.SecureRandom())
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.socketFactory)

    // Create all-trusting host name verifier
    val allHostsValid = HostnameVerifier …
Run Code Online (Sandbox Code Playgroud)

ssl https kotlin insecure-connection

6
推荐指数
1
解决办法
2816
查看次数