2

I am trying to do a GET some parameters to a server. I got this example from stackoverflow and I am facing a weird error. Can someone pls help me demystify the scenario ? Attached is the class file I am using

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();
  }
 }
}

And the error I am getting is

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)

Thanks a lot!

1 Answer 1

4

You need the Apache Commons Codec library 1.4 or above in your classpath.

This library contains Base64 implementation.

Exception in thread "main" java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.<init>(I)V
Sign up to request clarification or add additional context in comments.

1 Comment

I had a old codec jar in the path. Just replaced it with latest and all works good. Thanks a lot @Jarandinor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.