我想连接一个网站并用 Java 下载一些数据,但是它需要身份验证。我在网上搜索,发现我应该发送user:pass
到该站点。我正在这样做但是我无法登录你能给我一个提示吗!哪里不对?
URL url = new URL("https://www.quora.com/");
URLConnection conn = url.openConnection();
conn.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (X11; U; Linux x86_64; en-GB; rv:1.8.1.6) Gecko/20070723 Iceweasel/2.0.0.6 (Debian-2.0.0.6-0etch1)"
);
String userpass = "email@xyz.com" + ":" + "password";
String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
conn.setRequestProperty ("Authorization", basicAuth);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));