- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试调用 REST API,但出现以下异常(最后是 Complete StackTrace):
"javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target"
我向 API 提供商询问了这个问题,并说您正在使用的 SSL 库有问题。
你会如何解决这个问题?使用相同的代码(如下),我确实毫无问题地调用了 NIMBLE API REST,但在这种情况下它不起作用。然后,我没有任何文件证书(.cer)。
我需要停用 SSL 验证,因为安全方法是我已经拥有的 API KEY。
有什么建议吗?
非常感谢!
==== 控制台:====
POST https://api.nexalogy.com/project/create?api_key=XXXXXXXXXXXXXXXXXXXX
HTTP Header:"Content-Type" "application/json"
HTTP Body:[{"name":"project1","lang":"en","type":"twitter"}]
api_key:XXXXXXXXXXXXXXXXXXXX
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
我也试过下面的形式,异常(exception)是一样的:
POST https://api.nexalogy.com/project/create
HTTP Header:"Content-Type" "application/json"
HTTP Header:"api_key" "XXXXXXXXXXXXXXXXXXXX"
HTTP Body:[{"name":"project1","lang":"en","type":"twitter"}]
api_key:XXXXXXXXXXXXXXXXXXXX
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
==== 代码:====
package servlet;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/AddProject")
public class AddProject extends HttpServlet {
private static final long serialVersionUID = 1L;
public AddProject() {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String body="[{\"name\":\"project1\",\"lang\":\"en\",\"type\":\"twitter\"}]";
String api_key="XXXXXXXXXXXXXXXXXXXX";
String str_response="";
String line="";
URL url = new URL("https://api.nexalogy.com/project/create?api_key="+api_key);
try{
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type","application/json");
System.out.println("POST https://api.nexalogy.com/project/create?api_key="+api_key);
System.out.println("HTTP Header:"+"\"Content-Type\" \"application/json\"");
System.out.println("HTTP Body:"+body);
System.out.println("api_key:"+api_key);
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(body);
wr.flush();
wr.close();
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader rd = new BufferedReader(isr);
while ((line = rd.readLine()) != null) str_response+= line + '\r';
rd.close();
System.out.println("str_response:"+str_response);
}catch(Exception e){
e.printStackTrace(System.out);
//throw new RuntimeException(e);
}
}
}
==== 完整的 StackTrace:====
com.sun.jersey.api.client.ClientHandlerException: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:131)
at com.sun.jersey.api.client.Client.handle(Client.java:616)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:559)
at com.sun.jersey.api.client.WebResource.post(WebResource.java:230)
at engine_brandchats.twitter_api_create_project_0_1.Twitter_api_create_project.tREST_2Process(Twitter_api_create_project.java:955)
at engine_brandchats.twitter_api_create_project_0_1.Twitter_api_create_project.tJava_1Process(Twitter_api_create_project.java:635)
at engine_brandchats.twitter_api_create_project_0_1.Twitter_api_create_project.runJobInTOS(Twitter_api_create_project.java:1641)
at engine_brandchats.twitter_api_create_project_0_1.Twitter_api_create_project.main(Twitter_api_create_project.java:1494)
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(Unknown Source)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler$1$1.getOutputStream(URLConnectionClientHandler.java:203)
at com.sun.jersey.api.client.CommittingOutputStream.commitWrite(CommittingOutputStream.java:117)
at com.sun.jersey.api.client.CommittingOutputStream.write(CommittingOutputStream.java:89)
at sun.nio.cs.StreamEncoder.writeBytes(Unknown Source)
at sun.nio.cs.StreamEncoder.implFlushBuffer(Unknown Source)
at sun.nio.cs.StreamEncoder.implFlush(Unknown Source)
at sun.nio.cs.StreamEncoder.flush(Unknown Source)
at java.io.OutputStreamWriter.flush(Unknown Source)
at java.io.BufferedWriter.flush(Unknown Source)
at com.sun.jersey.core.util.ReaderWriter.writeToAsString(ReaderWriter.java:191)
at com.sun.jersey.core.provider.AbstractMessageReaderWriterProvider.writeToAsString(AbstractMessageReaderWriterProvider.java:128)
at com.sun.jersey.core.impl.provider.entity.StringProvider.writeTo(StringProvider.java:88)
at com.sun.jersey.core.impl.provider.entity.StringProvider.writeTo(StringProvider.java:58)
at com.sun.jersey.api.client.TerminatingClientHandler.writeRequestEntity(TerminatingClientHandler.java:305)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:182)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:129)
... 7 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(Unknown Source)
at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
at sun.security.validator.Validator.validate(Unknown Source)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate(Unknown Source)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
... 35 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
at java.security.cert.CertPathBuilder.build(Unknown Source)
... 41 more
已编辑 2013-06-13 解决方案:实现 X509TrustManager
添加以下代码...
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
if (connection instanceof HttpsURLConnection) {
try {
KeyManager[] km = null;
TrustManager[] tm = {new RelaxedX509TrustManager()};
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, tm, new java.security.SecureRandom());
SSLSocketFactory sf = sslContext.getSocketFactory();
((HttpsURLConnection)connection).setSSLSocketFactory(sf);
System.out.println("setSSLSocketFactory OK!");
}catch (java.security.GeneralSecurityException e) {
System.out.println("GeneralSecurityException: "+e.getMessage());
}
}
...并添加以下类(实现 X509TrustManager)
class RelaxedX509TrustManager implements X509TrustManager {
public boolean isClientTrusted(java.security.cert.X509Certificate[] chain){ return true; }
public boolean isServerTrusted(java.security.cert.X509Certificate[] chain){ return true; }
public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; }
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String input) {}
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String input) {}
}
最佳答案
试试这个
public void disableCertificates() {
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
}
}
关于java - 如何停用 SSL 验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17066321/
我找不到在来电时激活和停用振动的方法。 菜单中的选项 --> 设置 --> 声音和显示提到 - PHONE VIBRATE - 来电时手机振动... 我想通过代码激活和停用它(如果可能的话)。 最佳答
我有两个元素在彼此之上。当我点击第一个 div 上的按钮时,第二个 div 在第一个 div 之上打开,我想要做的是让底层 div 成为非交互式的(我不能点击底层 div 上的任何东西只要 overl
有没有办法取消 UIScrollView 的减速? 我想允许用户滚动 Canvas ,但我不希望用户抬起手指后 Canvas 继续滚动。 最佳答案 这可以通过利用 UIScrollView 委托(de
这里是关于 Stack Oveflow 的第一个问题,所以不要作恶! :) 言归正传:如果有堆叠的元素和堆叠的操作区域,如何继续操作以确保您对所看到的内容而不是底层元素进行操作? 我正在学习有关 Qt
这个问题已经有答案了: Deleting Objects in JavaScript (14 个回答) 已关闭 9 年前。 我有一个类,我通过以下方式调用: this.infiniteScroll =
我有一个优化问题,正在尝试使用 optaplanner 来解决。求解算法使用一组规则。引擎使用一个对象来捕获每个规则的权重。规则的最终得分是规则的中间得分乘以权重。分数设置在每条规则的右侧。每个规则的
有没有办法取消 UIScrollView 的减速? 我想允许用户滚动 Canvas ,但我不希望用户抬起手指后 Canvas 继续滚动。 最佳答案 这可以通过利用 UIScrollView 委托(de
我正在尝试更新 native android 应用程序,该应用程序以前是由其他一些人在某些跨平台技术(Titanium)中构建和上传的。应用程序以高级模式发布,其中针对平板电脑和手机有不同的构建。但现
我有一个 JList 列表和以下代码行: list.getInputMap().put(KeyStroke.getKeyStroke('d'), "action"); 因此,当我的列表处于焦点状态并且
有没有办法通过 SQL 语句停用 postgres 用户帐户? 我想阻止用户使用他们的数据库,但不删除用户或他们的数据库。 最佳答案 您还可以考虑 ALTER USER someone WITH NO
我有一个问题。我有一个 ViewController1,它通过 Push-segue 打开 ViewController2。//两者都是NavigationControllers - (void)pr
当我去 Playground 写 let test = "\u{062F}\u{0625} Hello" 时,我得到 Hello دإ(通过当我从输出控制台复制到这里时,我得到 دإ Hello) 似
我想通过扬声器播放歌曲,同时能够使用 Quickblox 接听视频通话。 我的音频速率越来越乱了。还有一个更大的问题是,当通话结束时,quickblox 框架将 Audio Session 设置为停用
我有一个工作项目,我以 tomcat 用户身份登录,但我不知道如何注销,我尝试停用 tomcat session ,我们使用 java spring,这是我尝试从 Controller : @Requ
我正在使用 javascript 来缩放我的 asp.net 网页上的图像。我想在上面放 2 个按钮,例如“缩放”、“取消缩放”,并相应地激活/停用 javascript 功能。现在我有一个 java
我有一个 TextField 和一个按钮。此 TextField 最多可包含 3 个字母或数字。 这是我的问题。当程序运行时,如果这个文本字段为空或者如果这个文本字段不只包含数字,我希望我的按钮被禁用
我有几个组,可以选择三个按钮。我试图做到这一点,以便当有人选择 N/A 按钮时,它会禁用其他两个按钮。当取消选择 N/A 按钮时,将启用其他两个按钮。我让它在我的机器上工作,其他两个按钮被着色为禁用,
HTML: Button Main Menu A Main Menu B
我是 php 新手,如何才能完成这项工作 删除.php prepare("UPDATE tbluser set status=1 WHERE id=:id"); $stmt->execute(
周五,我开始在运行 Ubuntu 14.04 的 VPS 上编写我的第一个 python API hello world 示例。我使用 python3,创建文件夹,virtualenv,激活它,然后断
我是一名优秀的程序员,十分优秀!