简体   繁体   English

org.apache.http.ssl.TrustStrategy上的ClassNotFoundException

[英]ClassNotFoundException on org.apache.http.ssl.TrustStrategy

I'm trying to run a plugin that makes HTTP/HTTPS POST requests. 我正在尝试运行一个发出HTTP / HTTPS POST请求的插件。 On it its declared the needed dependencies, that is httpclient and httpcore . 在它上面声明了所需的依赖关系,即httpclienthttpcore I'm using versions 4.5.3 and 4.4.6 respectively. 我分别使用版本4.5.3和4.4.6。 Although imported correctly all (I mean), I got this error on execution time: 尽管导入正确(我的意思是),我在执行时遇到了这个错误:

Caused by: java.lang.NoClassDefFoundError: 
org/apache/http/ssl/TrustStrategy
25.06 19:59:12 [Server] INFO at 
com.b5team.postrequest.Main.onCommand(Main.java:77) ~[?:?]
25.06 19:59:12 [Server] INFO at 
org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~
[Spigot.jar:git-Spigot-3fb9445-6e3cec8]
25.06 19:59:12 [Server] INFO ... 10 more
25.06 19:59:12 [Server] INFO Caused by: 
java.lang.ClassNotFoundException: org.apache.http.ssl.TrustStrategy

And here is my code: 这是我的代码:

package com.b5team.postrequest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;

public class SocketPOSTRequest {

    public void sendRequest(String myurl, String hash, String args[]) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, ClientProtocolException, IOException {

        HttpClientBuilder b = HttpClientBuilder.create();

        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {

            public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                return true;
            }
        }).build();

        b.setSSLContext(sslContext);

        HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;

        SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", sslSocketFactory)
                .build();

        PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
        b.setConnectionManager(connMgr);

        HttpClient client = b.build();
        HttpPost post = new HttpPost(myurl);

        List<NameValuePair> params = new ArrayList<NameValuePair>(args.length);
        params.add(new BasicNameValuePair("hash", hash));

        for(int i = 0; i < args.length; i++) {
            params.add(new BasicNameValuePair("arg"+i, args[i]));
        }

        post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
        HttpResponse response = client.execute(post);
        HttpEntity entity = response.getEntity();

        if (entity != null) {

            InputStream in = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String line;

            System.out.println("[POSTRequest] Data sent successfully!");

            while ((line = reader.readLine()) != null) {
                System.out.println("[POSTRequest] Report: "+line);
            }
        }
    }
}

EDIT: I'm using Ant to build, and the dependencies are correctly added. 编辑:我正在使用Ant来构建,并正确添加了依赖项。 I tested too with Maven, adding the dependencies, but the error remains. 我也测试了Maven,添加了依赖项,但错误仍然存​​在。

EDIT2: Switched to Maven, added maven-shade-plugin and maven-compile-plugin. EDIT2:切换到Maven,添加了maven-shade-plugin和maven-compile-plugin。 The error disappeared, but now got this java.lang.NoSuchMethodError: org.apache.http.impl.client.HttpClientBuilder.setSSLContext . 错误消失了,但现在得到了这个java.lang.NoSuchMethodError: org.apache.http.impl.client.HttpClientBuilder.setSSLContext When running with junit, don't occurs any errors. 使用junit运行时,不要发生任何错误。 It only occurs when running on server, that is Spigot 1.11.2 Minecraft Server. 它只发生在服务器上运行时,即Spigot 1.11.2 Minecraft Server。

if you are using a maven project, add the below dependency in your pom.xml file. 如果您使用的是maven项目,请在pom.xml文件中添加以下依赖项。

 <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.3</version>
</dependency>

So, I replaced the sslcontext methods from apache httpclient and httpcore with javax sslcontext methods. 所以,我用apache httpclient和httpcore用javax sslcontext方法替换了sslcontext方法。 Now, everything works fine. 现在,一切正常。 Remembering, that the above code was working normally on pure java. 记住,上面的代码在纯java上正常工作。 The real problem was when running on Minecraft server. 真正的问题是在Minecraft服务器上运行时。

Anyway, i will put below the new code, for documentation, maybe helps someone. 无论如何,我将在新代码下面,为文档,也许帮助某人。

package com.b5team.postrequest;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class HttpsPOSTRequest {

    public static void sendRequest(String myurl, String hash, String args[]) throws NoSuchAlgorithmException, KeyManagementException {

        try {
            SSLContext context = SSLContext.getInstance("TLS");
            context.init(new KeyManager[0], new TrustManager[] {new DefaultTrustManager()}, new SecureRandom());
            SSLContext.setDefault(context);

            URL url = new URL(myurl);
            HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
            con.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }
            });

            con.setRequestMethod("POST");
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0;Windows98;DigExt)");
            con.setDoOutput(true);
            con.setDoInput(true);

            ArrayList<String> params = new ArrayList<String>(args.length + 1);

            DataOutputStream output = new DataOutputStream(con.getOutputStream());
            output.writeBytes("hash=" + hash);
            for(int i = 0; i < params.size(); i++) {
                output.writeBytes("&");
                output.writeBytes("arg" + i + "=" + args[i]);
                output.flush();
            }

            output.flush();
            output.close();

            DataInputStream input = new DataInputStream(con.getInputStream());
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            String line;

            System.out.println("[POSTRequest] Data sent successfully!");
            System.out.println("[POSTRequest] Resp Code:"+con.getResponseCode());
            System.out.println("[POSTRequest] Resp Message:"+con.getResponseMessage());

            while ((line = reader.readLine()) != null) {
                System.out.println("[POSTRequest] Report: "+line);
            }

            input.close();

        } catch (UnsupportedEncodingException e) {
            System.out.println("[POSTRequest] Encoding error. Maybe string have invalid caracters.");
            e.printStackTrace();
        } catch (MalformedURLException e) {
            System.out.println("[POSTRequest] Invalid URL. Verify your URL and try again.");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("[POSTRequest] Error on HTTPS connection.");
            e.printStackTrace();
        }
    }

    private static class DefaultTrustManager implements X509TrustManager {

        @Override
        public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}

        @Override
        public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Apache HttpClientBuilder生成ClassNotFoundException:org.apache.http.config.Lookup - Apache HttpClientBuilder produces ClassNotFoundException: org.apache.http.config.Lookup java.lang.ClassNotFoundException:HttpPost 上的 org.apache.http.HttpEntity - java.lang.ClassNotFoundException: org.apache.http.HttpEntity on HttpPost ClassNotFoundException:org.apache.axis2.transport.http.AxisAdminServlet - ClassNotFoundException: org.apache.axis2.transport.http.AxisAdminServlet java.lang.ClassNotFoundException:org.apache.http.config.RegistryBuilder - java.lang.ClassNotFoundException: org.apache.http.config.RegistryBuilder Java Firebase错误“ NoClassDefFoundError:org / apache / http / conn / ssl / StrictHostnameVerifier” - Java Firebase error “NoClassDefFoundError: org/apache/http/conn/ssl/StrictHostnameVerifier” 找不到 Java org.apache.http.ssl.SSLContext - Java org.apache.http.ssl.SSLContext not found org.apache.http.conn.ssl上的java.lang.NoClassDefFoundError org / apache / commons / logging / LogFactory - java.lang.NoClassDefFoundError org/apache/commons/logging/LogFactory at org.apache.http.conn.ssl HTTP状态500-java.lang.ClassNotFoundException:org.apache.jsp.index_jsp - HTTP Status 500 - java.lang.ClassNotFoundException: org.apache.jsp.index_jsp 硒与Crome和Maven不起作用。 ClassNotFoundException:org.apache.http.auth.Credentials - Selenium with Crome and Maven not works. ClassNotFoundException: org.apache.http.auth.Credentials unirest-java.lang.ClassNotFoundException:org.apache.http.nio.reactor.IOReactorException - unirest - java.lang.ClassNotFoundException: org.apache.http.nio.reactor.IOReactorException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM