我正在尝试从 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,但仍然遇到相同的错误。我错过了什么吗?
最佳答案
您需要导入 org.jsoup.Connection 而不是 java.sql.Connection。请参阅文档:https://jsoup.org/apidocs/org/jsoup/Connection.html
关于java - 找不到符号,接口(interface)连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54796737/