嗨,我有类似以下内容的内容,可以创建 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();
我试过这样的东西,但它似乎没有用......
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);