与基本访问validation的Jsoup连接
Jsoup有没有办法从基本访问validation的网站加载文档?
使用HTTP基本访问身份validation,您需要发送Authorization
标头以及值"Basic " + base64encode("username:password")
。
例如(几乎没有Apache Commons Codec Base64的帮助):
String username = "foo"; String password = "bar"; String login = username + ":" + password; String base64login = new String(Base64.encodeBase64(login.getBytes())); Document document = Jsoup .connect("http://example.com") .header("Authorization", "Basic " + base64login) .get(); // ...
( getBytes()
的字符编码的明确规定为简明起见而省略,因为login名和pass通常是简单的US-ASCII
;此外,Base64总是生成US-ASCII
字节)
//Log in Response res = Jsoup .connect("url") .data("loginField", "login") .data("passwordField", "password") .method(Method.POST) .execute(); Document doc = res.parse(); //Keep logged in Map<String, String> cookies = res.cookies(); Document doc2 = Jsoup .connect("url") .cookies(cookies) .get();