[英]HttpsURLConnection.getInputStream works fine in Eclipse but server returns response code 503 after code is exported and run from command line
I use Eclipse and occasionally export my program and run it from the command line.我使用 Eclipse 并偶尔导出我的程序并从命令行运行它。 The functionality is exactly the same except this one thing:除了这一点之外,功能完全相同:
HttpsURLConnection
reads some data from a website. HttpsURLConnection
从网站读取一些数据。 This works fine when I run the code from Eclipse.当我从 Eclipse 运行代码时,这工作正常。 However, after I export my code as Runnable JAR, this part of the program does not work: I get a但是,在我将代码导出为 Runnable JAR 后,这部分程序不起作用:我得到一个
java.io.IOException: Server returned HTTP response code: 503
Here is the method that triggers this error:这是触发此错误的方法:
private List<Event> parse(final String url) throws IOException {
InputStream inputStream = null;
try {
URL u = new URL(url);
HttpsURLConnection con = (HttpsURLConnection)u.openConnection();
con.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; Windows NT 6.1)");
inputStream = con.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
}
final String page
= org.apache.commons.io.IOUtils.toString(inputStream, java.nio.charset.StandardCharsets.US_ASCII);
inputStream.close();
...
}
Please note that my program only visits this website once.请注意,我的程序只访问该网站一次。 It does not "harass" the server...它不会“骚扰”服务器......
I replaced java
in the command line with "C:\Program Files\Java\jdk-13.0.1\bin\java.exe"
and now it works.我将命令行中的java
替换为"C:\Program Files\Java\jdk-13.0.1\bin\java.exe"
,现在它可以工作了。
[I found out that's what Eclipse was doing by going to Project → Properties → Run/Debug Settings → Edit → Show Command Line]. [我通过转到项目→属性→运行/调试设置→编辑→显示命令行发现Eclipse正在做的事情]。
I am not sure where exactly the plain java
I was using before was pointing - and at this point, I don't intend to investigate.我不确定我之前使用的普通java
到底指向哪里——在这一点上,我不打算调查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.