创建一个可以在JAAS Security中传递用户名和密码的URLConnection

2 java proxy proxy-authentication

嗨,我有类似以下的东西,使URL连接并检索数据.问题是,当我点击需要身份验证的页面时,我会获得登录页面.我不确定如何在URLConnection中传递用户名和密码.我正在使用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();
Run Code Online (Sandbox Code Playgroud)

我试过这样的东西,但似乎没有用......

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);
Run Code Online (Sandbox Code Playgroud)

小智 6

而不是"代理授权",请尝试"授权".在对代理进行身份验证时使用"代理授权"标头,而不是您尝试访问的实际Web服务器.