我试图点击https网址并下载该文件。 以下是代码。
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.io.*;
import java.net.*;
public class SampleTest {
public static void main(String[] args) throws Exception {
URL url;
InputStream is = null;
BufferedReader br;
String line;
// Install Authenticator
MyAuthenticator.setPasswordAuthentication("myUsername", "myPassword");
Authenticator.setDefault (new MyAuthenticator ());
try {
url = new URL("https://myURL/");
is = url.openStream();
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (is != null) is.close();
} catch (IOException ioe) {
// nothing to see here
}
}
}
}
class MyAuthenticator extends Authenticator {
private static String username = "";
private static String password = "";
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(MyAuthenticator.username,
MyAuthenticator.password.toCharArray());
}
public static void setPasswordAuthentication(String username, String password) {
MyAuthenticator.username = username;
MyAuthenticator.password = password;
}
}
以下是生成的异常:
java.io.IOException: Server returned HTTP response code: 403 for URL:
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at java.net.URL.openStream(URL.java:1045)
at SampleTest.main(SampleTest.java:20)
请指教。我尝试了所有可能的方法来验证和访问URL,但没有运气。以前任何人都面临同样的问题。
- 已编辑 - 尝试使用以下代码。连接的语句已打印,然后是异常。
public static void main(String args[]) throws Exception{
URLConnection connection = new URL("https://myURL/").openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
connection.connect();
System.out.print("--connected---");
BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));
System.out.print("--rrr---" + r);
StringBuilder sb = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
sb.append(line);
}
System.out.println(sb.toString());
}
例外:
--connected---Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: https://myURL
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at FinalTest.main(FinalTest.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
答案 0 :(得分:0)
我尝试模拟您的问题并设法在通过HTTPS进行身份验证时重现相同的错误,但之后我设法通过设置系统属性来解决它,如下所示。
System.setProperty(" http.agent"," Mozilla / 4.76");
P.S我的JDK设置为1.8