Jsoup与基本访问身份验证的连接 [英] Jsoup connection with basic access authentication

查看:121
本文介绍了Jsoup与基本访问身份验证的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Jsoup中是否有办法从具有基本访问认证的网站加载文档?

Is there a way in Jsoup to load a document from a website with basic access authentication?

推荐答案

使用 HTTP基本访问身份验证您需要发送授权标头以及值基本+ base64encode(用户名:密码)

With HTTP basic access authentication you need to send the Authorization header along with a value of "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()中的编码,因为登录名和传递通常是普通的 US-ASCII ;此外, Base64始终生成 US-ASCII 字节)

(explicit specification of character encoding in getBytes() is omitted for brevity as login name and pass is often plain US-ASCII anyway; besides, Base64 always generates US-ASCII bytes)

这篇关于Jsoup与基本访问身份验证的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆