使用Rest Template的连接请求后获取I / O错误超时

时间:2019-11-25 11:42:09

标签: api spring-boot jenkins resttemplate

在Jenkins上发布删除作业的请求时出现超时错误。该作业被删除,但没有在我的代码中得到响应。 尝试增加和减少超时时间,但抛出400错误的请求错误 删除仅在5000超时时有效,但是在发布请求时出现I / O错误。

我为Rest Template手动创建实例,因为每个请求的凭据都不同

P.S:通过邮递员在本地运行时,此方法效果很好。问题仅在远程发生

下面是我的代码

公共类JenkinAuthRestTemplate扩展了RestTemplate {

public JenkinAuthRestTemplate(String password, int readTimeOut, int connectTimeOut) {

    addAuthentication(password, readTimeOut, connectTimeOut);
}

private void addAuthentication(String password, int readTimeOut, int connectTimeOut) {

    SSLContext sslContext = null;
    try {
        sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
    } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new HostnameVerifier() {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    });

    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    requestFactory.setConnectTimeout(readTimeOut);
    requestFactory.setReadTimeout(connectTimeOut);
    requestFactory.setHttpClient(HttpClients.custom().setSSLSocketFactory(sslsf).build());

    List<ClientHttpRequestInterceptor> interceptors = Collections
            .<ClientHttpRequestInterceptor> singletonList(new BasicAuthorizationInterceptor(password));
    setRequestFactory(new InterceptingClientHttpRequestFactory(requestFactory, interceptors));
}

private static class BasicAuthorizationInterceptor implements ClientHttpRequestInterceptor {


    private final String password;


    public BasicAuthorizationInterceptor(String password) {

        this.password = (password == null ? "" : password);
    }

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
            throws IOException {

        request.getHeaders().add("Authorization", "Basic " + (this.password));
        return execution.execute(request, body);
    }
}

}

deleteJob.java

JenkinAuthRestTemplate restTemplate = new JenkinAuthRestTemplate(                 jenkin.getToken(endpoint),5000,5000);     ResponseEntity响应= restTemplate.exchange(端点,HttpMethod.POST,null,String.class); logger.info(response.getStatusCodeValue());

0 个答案:

没有答案