有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java创建一个URLConnection,可以在JAAS安全中传递用户名和密码

嗨,我有下面这样的东西,可以建立一个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);

共 (1) 个答案

  1. # 1 楼答案

    请尝试“授权”,而不是“代理授权”。“Proxy Authorization”(代理授权)标头用于针对代理而不是您试图访问的实际web服务器进行身份验证