在JAAS Security中创建可以传递用户名和密码的URLConnection
您好,我有类似以下内容的内容可以创建 URL 连接 并检索数据。 问题 当我点击一个页面时 我首先需要身份验证 登录页面。 我不确定如何 在中传递用户名和密码 URL连接。 我正在访问一个网站 那就是使用 JAAS 验证。
import java.net.URL;
import java.net.URLConnection;
.....
URL location = new URL("www.sampleURL.com");
URLConnection yc = location.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
data += inputLine + "\n";
in.close();
我尝试过类似的方法,但似乎不起作用......
URL url = new URL("www.myURL.com");
URLConnection connection = url.openConnection();
String login = "username:password";
String encodedLogin = new BASE64Encoder().encodeBuffer(login.getBytes());
connection.setRequestProperty("Proxy-Authorization", "Basic " + encodedLogin);
Hi I have something like the following that makes a URL Connection
and retrieves the data. The problem
is that when I hit a page that
requires authentication first I get
the login page. I'm unsure of how to
pass a username and password in the
URLConnection. I'm hitting a site
that is using JAAS authenticaiton.
import java.net.URL;
import java.net.URLConnection;
.....
URL location = new URL("www.sampleURL.com");
URLConnection yc = location.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
data += inputLine + "\n";
in.close();
I tried something like this but it didn't seem to work...
URL url = new URL("www.myURL.com");
URLConnection connection = url.openConnection();
String login = "username:password";
String encodedLogin = new BASE64Encoder().encodeBuffer(login.getBytes());
connection.setRequestProperty("Proxy-Authorization", "Basic " + encodedLogin);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试“授权”,而不是“代理授权”。 当针对代理而不是您尝试访问的实际 Web 服务器进行身份验证时,将使用“Proxy-Authorization”标头。
Instead of "Proxy-Authorization", try "Authorization". The "Proxy-Authorization" header is used when authenticating against a proxy rather than the actual web server you are trying to access.