Results 1 to 5 of 5
  1. #1
    jdetloff is offline Member
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Default Using HttpClient with Eclipse

    I was recently told that I should be using HttpClient to solve a particular problem I was working on. I've never used it before, and have a lot to learn about it, but before I can start I need to download it.

    And embaressingly enough thats where I'm stuck.

    I went the download page at the httpclient site (wont let my put link here)
    and "right click- save as" the .zip file under binary with dependencies. I extracted it into a project file, and then tried to run the sample code:

    import org.apache.commons.httpclient.*;
    import org.apache.commons.httpclient.methods.*;
    import org.apache.commons.httpclient.params.HttpMethodPar ams;

    import java.io.*;

    public class HttpClientTutorial {

    private static String url = "website"; //website instead of a real website,
    //cause i cant post links

    public static void main(String[] args) {
    // Create an instance of HttpClient.
    HttpClient client = new HttpClient();

    // Create a method instance.
    GetMethod method = new GetMethod(url);

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.R ETRY_HANDLER,
    new DefaultHttpMethodRetryHandler(3, false));

    try {
    // Execute the method.
    int statusCode = client.executeMethod(method);

    if (statusCode != HttpStatus.SC_OK) {
    System.err.println("Method failed: " + method.getStatusLine());
    }

    // Read the response body.
    byte[] responseBody = method.getResponseBody();

    // Deal with the response.
    // Use caution: ensure correct character encoding and is not binary data
    System.out.println(new String(responseBody));

    } catch (HttpException e) {
    System.err.println("Fatal protocol violation: " + e.getMessage());
    e.printStackTrace();
    } catch (IOException e) {
    System.err.println("Fatal transport error: " + e.getMessage());
    e.printStackTrace();
    } finally {
    // Release the connection.
    method.releaseConnection();
    }
    }
    }


    I got this error in the console -

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    HttpClient cannot be resolved to a type
    HttpClient cannot be resolved to a type
    GetMethod cannot be resolved to a type
    GetMethod cannot be resolved to a type
    HttpMethodParams cannot be resolved
    DefaultHttpMethodRetryHandler cannot be resolved to a type
    HttpStatus cannot be resolved
    HttpException cannot be resolved to a type

    at Test.main(Test.java:14)

    When hovering over my import statements it tells me that the imports can't be resolved.

    Any ideas what I'm doing wrong? I assume the .jars are in the wrong place? The website also had something about "verify the integrity of the downloaded files using signatures downloaded from our main distribution directories." and "The KEYS link links to the code signing keys used to sign the product. The PGP link downloads the OpenPGP compatible signature from our main site. The MD5 link downloads the checksum from the main site. " which I honestly don't know what means.

    Anyways, I'm sure there are some serious flaws in my logic here, but any guidance would be greatly appreciated, I'm a beginner struggling to find a place to start learning.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    The jars containing those classes need to be added to the classpath when you compile (and run) the program.

    Setting the class path

  3. #3
    jdetloff is offline Member
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Default

    Hmmm... I checked the .classpath file in the project folder and they're definately in it. Could it be anything else? What does it mean for some classes to be dependencies?

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by jdetloff View Post
    Hmmm... I checked the .classpath file in the project folder and they're definately in it. Could it be anything else? What does it mean for some classes to be dependencies?
    Do you see those classes if you open the jars that are on your classpath?

  5. #5
    jdetloff is offline Member
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Default

    Actually, when opening the jars none of them had a httpclient inside a commons folder, which is confusing, because I downloaded this directly from the httpclient site, and theres even a quickstart page that lists which .jar files should be included.

    Is it possible this has something to do with the "verify the integrity of the downloaded files using signatures downloaded from our main distribution directories" statement on the download page? What does that mean?

Similar Threads

  1. Getting httpclient working with eclipse.
    By int80 in forum Eclipse
    Replies: 4
    Last Post: 03-31-2009, 02:17 PM
  2. HttpClient invalid headers
    By Nicole in forum Advanced Java
    Replies: 6
    Last Post: 03-31-2009, 01:36 AM
  3. Replies: 1
    Last Post: 03-13-2009, 10:10 PM
  4. Replies: 2
    Last Post: 01-27-2009, 03:10 PM
  5. Eclipse Bug - Can't Read From A File Using Eclipse?
    By carlodelmundo in forum New To Java
    Replies: 6
    Last Post: 01-26-2009, 05:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •