apache httpget破坏无一例外

时间:2017-03-11 11:31:05

标签: java apache http exception

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

对于它来说运行正常。

但是在服务器上它会一直没有任何异常,除了因为空结果而无效的空指针

我正在使用apache http lib。

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

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);
        }
    }
}

以下是日志snipets;我试图掩盖每一个明智的数据,我希望我没有错过任何东西

Local log snipet

Remote log snipet

正如你所看到的那样,数字在8之后停止,然后再次以14开始在finally块中。剩下的就丢失了,我不知道,为什么。

可以通过浏览器或comandline访问使用过的URL。

1 个答案:

答案 0 :(得分:1)

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

但是当你正在追赶 Exception 时,这段代码根本就没有“看到”真正的问题。

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