我正在尝试从Java代码登录网页。 我有Jsoup软件包,但我不断收到错误消息:
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
这就是所有代码:
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());
}
}
因此,我已经从Java导入了Connection-package,但是仍然遇到相同的错误。我错过了什么吗?
答案 0 :(得分:0)
您需要导入org.jsoup.Connection而不是java.sql.Connection。查看文档:{{3}}