gpt4 book ai didi

java - Apache HttpClient 代理问题

转载 作者:行者123 更新时间:2023-12-04 04:55:32 28 4
gpt4 key购买 nike

我正在尝试对服务器执行 GET 一些参数。我从 stackoverflow 得到了这个例子,但我遇到了一个奇怪的错误。有人可以帮我揭开这个场景的神秘面纱吗?附件是我正在使用的类文件

import java.io.IOException;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpClientTest {
private static final String PROXY_HOST = "proxy.domain";
private static final int PROXY_PORT = 8080;

public static void main(String[] args) {

try{
String username = "username";
String password = "Password";
DefaultHttpClient httpclient = new DefaultHttpClient();
org.apache.http.auth.Credentials credentials_new = new org.apache.http.auth.UsernamePasswordCredentials(username, password) ;

CredentialsProvider cp = httpclient.getCredentialsProvider();
cp.setCredentials(new AuthScope(PROXY_HOST, PROXY_PORT),credentials_new);
HttpHost proxy = new HttpHost(PROXY_HOST, PROXY_PORT);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
HttpGet httpget = new HttpGet("http://www.google.com");
HttpResponse response = httpclient.execute(httpget);


System.out.println("Response: " + response.getStatusLine());
} catch (ClientProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

我得到的错误是
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.<init>(I)V
at org.apache.http.impl.auth.GGSSchemeBase.<init>(GGSSchemeBase.java:75)
at org.apache.http.impl.auth.SPNegoScheme.<init>(SPNegoScheme.java:47)
at org.apache.http.impl.auth.SPNegoSchemeFactory.newInstance(SPNegoSchemeFactory.java:55)
at org.apache.http.auth.AuthSchemeRegistry.getAuthScheme(AuthSchemeRegistry.java:114)
at org.apache.http.impl.client.AuthenticationStrategyImpl.select(AuthenticationStrategyImpl.java:180)
at org.apache.http.impl.client.HttpAuthenticator.authenticate(HttpAuthenticator.java:144)
at org.apache.http.impl.client.DefaultRequestDirector.handleResponse(DefaultRequestDirector.java:1085)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:548)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)
at HttpClientTest.main(HttpClientTest.java:36)

非常感谢!

最佳答案

您需要 Apache Commons Codec library 1.4 或更高版本在您的类路径中。

该库包含 Base64 实现。

Exception in thread "main" java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.<init>(I)V

关于java - Apache HttpClient 代理问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16796039/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com