【问题标题】:apache httpget breaks without exceptionapache httpget 无一例外地中断
【发布时间】:2017-03-11 11:31:05
【问题描述】:

我写了一个小程序,通过 http-get 读取 XML 文件。

本地运行良好。

但在服务器上它一直在中断,没有任何异常,除了一个空指针,因为结果为空

我正在使用 apache http 库。

这是课程,我添加了数字,以跟踪它停止工作的确切点。

public void get(String url, String user, String pass, File outfile) throws IOException
{
    log.info("1");
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, "basic"),
        new UsernamePasswordCredentials(user, pass));

    log.info("2");
    CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();

    HttpResponse response = null;
    log.info("6");
    InputStream content = null;
    FileOutputStream outputStream = null;
    try
    {
        HttpGet httpGet = new HttpGet(url);
        log.info("3");
        httpGet.addHeader("Content-Type", "text/xml");
        log.info("4");
        httpGet.addHeader("Accept", "text/xml");
        log.info("5");

        log.info("6");

        log.info("7");

        outputStream = new FileOutputStream(outfile);
        log.info("8");
        response = httpClient.execute(httpGet);
        log.info("9");
        log.info("response: {}", response);
        log.info("10");
        content = response.getEntity().getContent();
        log.info("11");
        log.info("content: {}", content);
        log.info("12");
        IOUtils.copy(content, outputStream);
        log.info("13");
    }
    catch (Exception e)
    {
        log.error("", e);
    }
    finally
    {
        try
        {
            log.info("14");
            log.info(String.valueOf(content));
            content.close();
            outputStream.close();
        }
        catch (IOException e)
        {
            log.error("Error while closing Streams", e);
        }
    }
}

这里是日志片段;我试图掩盖每一个合理的数据,我希望我没有错过任何东西

Local log snipet

Remote log snipet

如您所见,数字在 8 之后停止,并在 finally 块中再次以 14 开始。其余的都丢失了,我不知道为什么。

使用的 URL 可通过浏览器或命令行访问。

【问题讨论】:

  • 谢谢你,成功了。它缺少 org/apache/commons/codec/binary/Base64。但有趣的是,这只发生在服务器上。
  • 感谢您的快速接受。哦,太好了,您刚刚获得了投票特权。随意在我的答案上练习这样做:-)

标签: java apache http exception


【解决方案1】:

问题有点棘手:您在这里遇到了某种错误。可能是 NoClassDefFoundError 或类似的东西。

但是当您捕获 Exception 时,这段代码根本没有“看到”真正的问题。

所以要调试问题:要么检查服务器日志文件,要么更改代码以捕获 Throwable 而不是 Exception。

【讨论】:

    猜你喜欢
    • 2021-06-16
    • 2014-11-29
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多