由于“不受信任的服务器证书”,httpget 无法工作但它适用于 httppost

发布于 2024-11-29 02:01:14 字数 951 浏览 0 评论 0原文

我有一个代码需要访问下面的网站并提取文本。在项目的另一部分中,我使用 httpost 方法,它根据我发送的内容给我一个响应。然而,对于 httpget 函数,它突然给了我一个异常,说 javax.net.ssl.SSLEXCEPTION:Not trusted servercertificate。

有办法解决这个问题吗?我不明白为什么一种方法有效而另一种方法无效。谢谢

     //some code
    getText("https://iphone-radar.com/accounts/4e3f2c6659f25a0f8400000b");

}
    public static void getText(String uri) {

        HttpClient httpclient = new DefaultHttpClient();

        // Prepare a request object
        HttpGet httpget = new HttpGet(uri); 

        // Execute the request
        HttpResponse response;
        try {
            response = httpclient.execute(httpget);
            // Examine the response status


            // Get hold of the response entity
            HttpEntity entity = response.getEntity();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            android.util.Log.v("EXCEPTION","["+e.getMessage()+"]", e);



        }
    }

I have a code that needs to go to the website below and extract the text. In another part of the project I use the httpost method and it gives me a response based on what I've sent. However for the httpget function suddenly it gives me an exception saying javax.net.ssl.SSLEXCEPTION:Not trusted server certificate.

Is there a way to fix this? I don't understand why one method works while the other doesn't. Thanks

     //some code
    getText("https://iphone-radar.com/accounts/4e3f2c6659f25a0f8400000b");

}
    public static void getText(String uri) {

        HttpClient httpclient = new DefaultHttpClient();

        // Prepare a request object
        HttpGet httpget = new HttpGet(uri); 

        // Execute the request
        HttpResponse response;
        try {
            response = httpclient.execute(httpget);
            // Examine the response status


            // Get hold of the response entity
            HttpEntity entity = response.getEntity();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            android.util.Log.v("EXCEPTION","["+e.getMessage()+"]", e);



        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

轻拂→两袖风尘 2024-12-06 02:01:15

使用如下所示的内容来覆盖验证程序。使用 Android 2.2 测试的代码

 /**
   * Will cause HttpsURLConnection to accept even self-signed certificates.
   * @param conn
   */
  private static void trustEveryone(HttpsURLConnection conn) {
    try {
      conn.setHostnameVerifier(new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
          return true;
        }
      });
      SSLContext context = SSLContext.getInstance("TLS");
      context.init(null, new X509TrustManager[] { new X509TrustManager() {

        @Override
        public void checkClientTrusted(
            java.security.cert.X509Certificate[] aChain, String aAuthType)
            throws java.security.cert.CertificateException {
        }

        @Override
        public void checkServerTrusted(
            java.security.cert.X509Certificate[] aChain, String aAuthType)
            throws java.security.cert.CertificateException {
        }

        @Override
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
          // TODO Auto-generated method stub
          return new java.security.cert.X509Certificate[0];
        }
      } }, new SecureRandom());
      conn.setSSLSocketFactory(context.getSocketFactory());
    } catch (Exception e) { //handle accordingly
      e.printStackTrace();
    }
  }

Use something like below to override the verifier. Code tested using Android 2.2

 /**
   * Will cause HttpsURLConnection to accept even self-signed certificates.
   * @param conn
   */
  private static void trustEveryone(HttpsURLConnection conn) {
    try {
      conn.setHostnameVerifier(new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
          return true;
        }
      });
      SSLContext context = SSLContext.getInstance("TLS");
      context.init(null, new X509TrustManager[] { new X509TrustManager() {

        @Override
        public void checkClientTrusted(
            java.security.cert.X509Certificate[] aChain, String aAuthType)
            throws java.security.cert.CertificateException {
        }

        @Override
        public void checkServerTrusted(
            java.security.cert.X509Certificate[] aChain, String aAuthType)
            throws java.security.cert.CertificateException {
        }

        @Override
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
          // TODO Auto-generated method stub
          return new java.security.cert.X509Certificate[0];
        }
      } }, new SecureRandom());
      conn.setSSLSocketFactory(context.getSocketFactory());
    } catch (Exception e) { //handle accordingly
      e.printStackTrace();
    }
  }
月牙弯弯 2024-12-06 02:01:15

如果您想简单地忽略所有 SSL 证书检查。以下链接的评论对我有帮助!

http://code.google.com/p/android/issues /detail?id=1946#c10

If you want to simply ignore all the SSL cert check. The comment on the following link helped me!

http://code.google.com/p/android/issues/detail?id=1946#c10

灼疼热情 2024-12-06 02:01:14

我在为学校开发应用程序时遇到了类似的问题。诀窍是创建两个覆盖证书验证的类。
一种扩展 HostnameVerifier 并在每次调用 verify() 时返回 true,例如 这个
另一个类扩展 X509TrustManager 并重写 getAcceptedIssuers(),如 这个.

然后,您可以使用以下代码将 HttpsURLConnection 设置为接受所有证书:

HttpsURLConnection.setDefaultHostnameVerifier(new AllVerifier());
try {
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, new TrustManager[] { new AllTrustManager() }, null);
    HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
} catch (KeyManagementException e) {
    e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
}

这应该可以解决问题。您可以看到我如何使用此代码 此处,在 run() 方法中。

I had a similar problem while developing an app for my school. The trick is to create two classes that override certificate verification.
One extends HostnameVerifier and returns true every time verify() is called, like this.
The other class extends X509TrustManager and overrides getAcceptedIssuers() like this.

Then you can set the HttpsURLConnection to accept all certificates using this code:

HttpsURLConnection.setDefaultHostnameVerifier(new AllVerifier());
try {
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, new TrustManager[] { new AllTrustManager() }, null);
    HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
} catch (KeyManagementException e) {
    e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
}

That should do the trick. You can see how I used this code here, in the run() method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文