Apache HttpClient代理问题

时间:2013-05-28 15:36:48

标签: java proxy httpclient http-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)

非常感谢!

1 个答案:

答案 0 :(得分:3)

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

该库包含Base64实现。

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