简体   繁体   English

HttpsURLConnection.getInputStream 在 Eclipse 中工作正常,但在导出代码并从命令行运行后服务器返回响应代码 503

[英]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.

相关问题 mysqldump从java运行时返回代码6,但同样的命令从命令行运行正常 - mysqldump returns code 6 when run from java, but the same command works fine from command line 如何提高从HttpsURLConnection.getInputStream()反序列化对象的性能? - How to improve performance of deserializing objects from HttpsURLConnection.getInputStream()? HttpsURLConnection.getInputStream()是否自动重试? - Does HttpsURLConnection.getInputStream() makes automatic retries? 代码在Eclipse中有效,但在命令行中无效 - code works in eclipse but not in command line 如何从命令行运行 java 项目(在 eclipse 中正常工作,gradle) - How to run java project from command line (works fine in eclipse, gradle) mvn install 在 Eclipse 中不起作用,但在命令行中工作正常 - mvn install is not working in eclipse but works fine from command line RMI-在Eclipse上运行客户端代码会引发MarshalException,而从控制台执行时效果很好 - RMI - run client code on Eclipse throws MarshalException while execute from console works fine VS Code 调试无法解析导入语句中的类,但使用命令行可以正常工作 - VS Code debug can't resolve classes from import statements while it works fine using command line 服务器返回URL的HTTP响应代码503 - Server returned HTTP response code 503 for URL 服务器返回HTTP响应代码503 - server returned HTTP response code 503
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM