Argo
Argo

Reputation: 153

JSoup POST authentication

I'm trying to connect to a site with basic username & password authentication, keep the cookies and parse data from a site that requires the cookies given at the login-page. The parsing works perfect when using a copy of the website from a local Apache webserver. It's the authentication that's giving me trouble. I'm using Eclipse as my IDE and I keep getting an error with the following piece of code:

    Response res = Jsoup
        .connect("site_with_login")
        .data("login", "MyUsername")
        .data("pass", "MyPassword")
        .method(Method.POST)
        .execute();

    //Keep logged in
    Map<String, String> cookies = res.cookies();

The .method(Method.POST) is the problem, because the error is at POST. Here's the output when running the parsing:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
POST cannot be resolved or is not a field

at parseHTML.main(parseHTML.java:26)

It's strange, because a lot of posts here on stackoverflow mention that excact way to authenticating. I have imported all the packaged and have the jsoup-1.7.2.jar file in my library.

Does anyone know how I can solve this annoying problem?

Thanks in advance.

Edit: I'm currently using the following packages:

    import org.jsoup.Connection.Method;
    import org.jsoup.Connection;
    import org.jsoup.Jsoup;
    import org.jsoup.helper.HttpConnection.Response;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;

The whole block of code I posted now gives the following error:

    Type mismatch: cannot convert from Connection.Response to HttpConnection.Response

The whole code is available here

Upvotes: 2

Views: 2607

Answers (1)

BobTheBuilder
BobTheBuilder

Reputation: 19284

I think you're using a wrong import.

Make sure you use:

.method(Connection.Method.POST)

Make sure you import: org.jsoup.Connection.Method and not java.lang.reflect.Method

Another import issue:

import org.jsoup.Connection.Response;

instead of:

import org.jsoup.helper.HttpConnection.Response;

Upvotes: 2

Related Questions