Search code examples
javajsoup

Cannot find symbol, Interface Connection


Im trying to login on a webpage from Java-code. I have the Jsoup-package, but I keep getting a error:

JSoupTitleEx.java:26: error: cannot find symbol
           Connection.Response loginForm = Jsoup.connect(loginFormUrl).method(Connection.Method.GET).userAgent(USER_AGENT).execute();
                     ^
  symbol:   class Response
  location: interface Connection

And this is all of the code:

import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;
import java.sql.Connection;

public class JSoupTitleEx {

   public static void main(String[] args) throws IOException {

     final String USER_AGENT = "\"Mozilla/5.0 (Windows NT\" +\n" +
     "          \" 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2\"";

       String loginFormUrl = "web";
       String loginActionUrl = "web";
       String username = "abcabc";
       String password = "blabla";
       HashMap<String, String> cookies = new HashMap<>();
       HashMap<String, String> formData = new HashMap<>();



       Connection.Response loginForm = Jsoup.connect(loginFormUrl).method(Connection.Method.GET).userAgent(USER_AGENT).execute();
       Document loginDoc = loginForm.parse(); // this is the document that contains response html


       cookies.putAll(loginForm.cookies()); // save the cookies, this will be passed on to next request

       formData.put("Username", username);
       formData.put("Password", password);

       Connection.Response homePage = Jsoup.connect(loginActionUrl)
      .cookies(cookies)
      .data(formData)
      .method(Connection.Method.POST)
      .userAgent(USER_AGENT)
      .execute();
      System.out.println(homePage.parse().html());

   }
}

So, I have imported the Connection-package from Java, but I still getting the same error. Have I missed something?


Solution

  • You need to import org.jsoup.Connection instead of java.sql.Connection. See docs: https://jsoup.org/apidocs/org/jsoup/Connection.html