Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android Studio中设置SignalR以忽略用于开发的SSL问题_Android_Ssl_Android Studio_Signalr_Ssl Certificate - Fatal编程技术网

如何在Android Studio中设置SignalR以忽略用于开发的SSL问题

如何在Android Studio中设置SignalR以忽略用于开发的SSL问题,android,ssl,android-studio,signalr,ssl-certificate,Android,Ssl,Android Studio,Signalr,Ssl Certificate,我有一个使用信号器的网站。下面的代码(取自SignalR聊天示例)通过其已验证的SSL连接到生产站点。它使用来自signar/java客户机的jar 但在开发过程中,我需要连接到VS项目,该项目使用IIS Express和本地SSL托管在我的计算机上,我的Nexus无法也不应该验证该SSL,我收到以下错误: 08-17 16:13:55.540 2153-6860//System.out﹕ HubConnection: Error executing request: java.secur

我有一个使用信号器的网站。下面的代码(取自SignalR聊天示例)通过其已验证的SSL连接到生产站点。它使用来自signar/java客户机的jar

但在开发过程中,我需要连接到VS项目,该项目使用IIS Express和本地SSL托管在我的计算机上,我的Nexus无法也不应该验证该SSL,我收到以下错误:

08-17 16:13:55.540    2153-6860//System.out﹕ HubConnection: Error executing request: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
08-17 16:13:55.541    2153-6859//System.err﹕ java.util.concurrent.ExecutionException: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
我已经打开了防火墙中的相关端口,可以通过Rest请求访问IIS Express

代码如下:

public class gtSockets {

    public static final String URL = myUrl + "/signalr/hubs/";

    HubConnection connection;
    HubProxy hub;

    DemonThread thread;

    private Context context;

    private static gtSockets gtSockets;

    private gtSockets(Context context) {
        this.context = context;

        initConnection();
    }

    public static gtSockets newInstance(Context context) {
        if(gtSockets == null) {
            gtSockets = new gtSockets(context);
        }
        return gtSockets;
    }

    private void initConnection() {
        Platform.loadPlatformComponent(new AndroidPlatformComponent());


        connection = new HubConnection(URL, null, true, new Logger() {
            @Override
            public void log(String s, LogLevel logLevel) {

            }
        });


        hub = connection.createHubProxy("myHub");


        hub.on("broadcastMessage", new SubscriptionHandler2<String, String>() {
            @Override
            public void run(String msgType, String msg) {

            }
                }, String.class,String.class);

        thread = new DemonThread();
        thread.start();
    }

    public void destroy() {
        if(thread != null) {
            thread.cancel();
            thread = null;
        }

        if(connection != null) {
            connection.stop();
        }
    }


    private class MessageSendTask extends AsyncTask<String,String,String> {
        @Override
        protected String doInBackground(String... params) {
            hub.invoke("send","directory",params[1]);
            return "ok";
        }
    }

    private class DemonThread extends Thread {

        boolean isRunning = true;

        @Override
        public void run() {
            SignalRFuture<Void> awaitConnection = connection.start();
            while(isRunning) {
                try {
                    awaitConnection.get();
                } catch (InterruptedException | ExecutionException e){
                    e.printStackTrace();
                }
            }
            awaitConnection.cancel();
        }

        public void cancel() {
            if(isRunning) {
                isRunning = false;
            }
        }
    }
}
公共类gtSockets{
公共静态最终字符串URL=myUrl+“/signalr/hubs/”;
轮毂连接;
hub代理中心;
DemonThread螺纹;
私人语境;
专用静态GT插座GT插座;
专用gtSockets(上下文){
this.context=上下文;
initConnection();
}
公共静态GTS实例(上下文){
if(gtSockets==null){
gtSockets=新的gtSockets(上下文);
}
返回插座;
}
私有连接(){
loadPlatformComponent(新的AndroidPlatformComponent());
connection=new-HubConnection(URL,null,true,new-Logger(){
@凌驾
公共无效日志(字符串s,日志级别LogLevel){
}
});
hub=connection.createhubbroxy(“myHub”);
hub.on(“broadcastMessage”,新的SubscriptionHandler2(){
@凌驾
公共无效运行(字符串msgType,字符串msg){
}
},String.class,String.class);
线程=新的DemonThread();
thread.start();
}
公共空间销毁(){
if(线程!=null){
线程。取消();
线程=null;
}
if(连接!=null){
连接。停止();
}
}
私有类MessageSendTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…参数){
调用(“发送”、“目录”、参数[1]);
返回“ok”;
}
}
私有类DemonThread扩展线程{
布尔值isRunning=true;
@凌驾
公开募捐{
SignalRFuture awaitConnection=connection.start();
同时(正在运行){
试一试{
等待连接。get();
}捕获(中断异常|执行异常e){
e、 printStackTrace();
}
}
等待连接。取消();
}
公开作废取消(){
如果(正在运行){
isRunning=false;
}
}
}
}
问题是我如何设置SignalR以忽略SSL问题进行开发


谢谢。

如果只是为了开发,我想您可以参考以下代码(在SO中提供):

公共类HttpsTrustManager实现X509TrustManager{
私有静态信任管理器[]信任管理器;
私有静态最终X509Certificate[]\u AcceptedIssuers=新X509Certificate[]{};
@凌驾
公共无效checkClientTrusted(
X509Certificate[]x509Certificates,字符串s)
抛出java.security.cert.CertificateException{}
@凌驾
公共无效检查服务器受信任(
X509Certificate[]x509Certificates,字符串s)
抛出java.security.cert.CertificateException{}
公共布尔值IsClient受信任(X509Certificate[]链){
返回true;
}
公共布尔值isServerTrusted(X509Certificate[]链){
返回true;
}
@凌驾
公共X509证书[]getAcceptedIssuers(){
返回-接受发行人;
}
公共静态void allowAllSSL(){
HttpsURLConnection.setDefaultHostnameVerifier(新的HostnameVerifier(){
@凌驾
公共布尔验证(字符串arg0,SSLSession arg1){
返回true;
}
});
SSLContext context=null;
if(trustManagers==null){
TrustManager=新的TrustManager[]{
新的HttpsTrustManager()
};
}
试一试{
context=SSLContext.getInstance(“TLS”);
init(null,trustManagers,new SecureRandom());
}catch(nosuchagorithmexception | KeyManagementException e){
e、 printStackTrace();
}
HttpsURLConnection.setDefaultSSLSocketFactory(context!=null?context.getSocketFactory():null);
}
}

然后在活动中,调用
HttpsTrustManager.allowAllSSL()在信号器方法调用之前。

@BNK我用截击库尝试了上述代码,但它给了我
BasicNetwork.performRequest:意外响应代码503
错误。那么截击是否需要任何额外设置?@Anil 503表示服务器服务不可用。请检查您的Web服务器。我可以知道如何在Kotlin中执行此操作吗?@y2kbug抱歉,实际上我不知道Kotlin。此外,我已经两年没有尝试过Android了:)