我正在尝试构建一个HttpClient,但不知道如何解决这个ClassNotFound-Error。我用maven和其他几个库安装了“org.apache.httpcomponents:httpclient:4.1.3”,但我还是不能工作。
代码:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.*;
import java.net.URISyntaxException;
import java.util.*;
public class Test {
public static void main(String[] args) {
String bearerToken = "BLABLA";
HttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build()).build();
}
}
字符串
相应的错误消息:
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/ssl/SSLContexts
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:977)
at Twitter.main(Twitter.java:23)
Caused by: java.lang.ClassNotFoundException: org.apache.http.ssl.SSLContexts
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
型
我目前安装的maven存储库:
oauth.signpost:signpost-commonshttp4:2.0.0
org.json:json:20200518
org.apache.httpcomponents:httpclient:4.1.3
型
请注意,我是Java和Intellij-IDE的新手。
谢谢你的帮助。
3条答案
按热度按时间tpgth1q71#
尝试使用“httpcore - 4.1.3”(与您的
httpclient
jar版本相同)jar文件。您将能够成功导入org.apache.http.ssl.SSLContexts
。hgqdbh6s2#
即使不导入
org.apache.http.ssl.SSLContexts
,也可能出现此错误尝试从maven存储库导入pom.xml文件中的
httpcore
包。也检查你有
httpclient
包。kjthegm63#
你有没有可能用
org.apache.httpcomponents httpclient 4.5.2
然后是
import org.apache.http.conn.ssl.SSLContexts