java中http客户端的ioexception

6psbrbz9  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(198)

我有一个在一组产品上循环的程序,对于每个产品,从外部应用程序请求一个xml。
我尝试过使用ApacheHTTP客户端和java-11HTTP客户端。两者都将在特定/固定数量的请求后引发ioexception。此ioexception在此之后重复多次。之后,请求突然停止抛出异常。
奇怪的是,这两段代码在经过104次之后开始抛出IOException。
对于apache方式,错误是在处理对{}->http://somesite:80: 连接重置
对于java11方式,http/1.1头解析器没有收到对等方的字节/连接重置。
如果我手动尝试下载导致问题的id为的xml,则响应正常。
所以,对我来说,我认为外部方关闭了连接或其他东西,但如何防止这种情况发生?我尝试使用connecttimeout/connectionrequesttimeout/sockettimeout,但没有解决问题。
谁有办法解决这个问题,谁能解释这里发生了什么?
apache变体的代码如下所示,每个产品/标识都会调用这些方法:

public String ApacheHTTP_getIceCatSpecifications(Integer Id)
{
    String list = new ArrayList<String>();

    CredentialsProvider provider = new BasicCredentialsProvider();
    provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(env.getProperty("user"), env.getProperty("password")));

    HttpGet httpGet = new HttpGet("http://somesite/" + Id + ".xml");

    CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();

    ICECATInterface product = null;
    try (CloseableHttpResponse response = client.execute(httpGet))
    {
        final HttpEntity entity = response.getEntity();
        if (entity != null)
        {
            InputStream inputStream = entity.getContent()
            //list = get from inputstream
        }
    } catch (ClientProtocolException e)
    {

    } catch (IOException e)
    {
        log.error("IOException {}");
    }
    return list;
}

对于java 11变体:

public List<String> getSpecifications_JAVA11(Integer Id)
{
    List<String> specificationList = new ArrayList<String>();

    HttpClient client = HttpClient.newBuilder()
            .version(HttpClient.Version.HTTP_1_1)
            .authenticator(new Authenticator()
            {
                @Override
                protected PasswordAuthentication getPasswordAuthentication()
                {
                    return new PasswordAuthentication(
                            env.getProperty("user"),
                            env.getProperty("password").toCharArray());
                }
            })
            .build();

    HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create("http://somesite/" + Id + ".xml"))
            .build();

    HttpResponse<InputStream> response = null;
    try
    {
        response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());

        // specificationList = read from the response...

    } catch (IOException e)
    {
        e.printStackTrace();
    } catch (InterruptedException e)
    {
        e.printStackTrace();
    }

    return specificationList;
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题