反正在Apache httpclient中重试/重复消耗的POST请求吗?

时间:2018-11-06 10:41:57

标签: java httpclient

我知道这很奇怪,但是就我而言,我想在第一个请求的状态为403时重复一个带有附加标头(实际上是凭据)的请求...

我正在按以下方式配置HttpClient,它适用于所有GET请求,但对于POST请求失败,具有以下异常。

.setServiceUnavailableRetryStrategy(new ServiceUnavailableRetryStrategy() {

                    @Override
                    public boolean retryRequest(HttpResponse response, int executionCount, HttpContext context) {
                        boolean shouldRetry = response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED
                                && executionCount < 2;
                        if (shouldRetry) {
                            _logger.warn("Got 401 status, retry request with vmware-api-session-id header");
                            context.setAttribute(REQ_ATTR_USING_SESSIONID_HEADER, Boolean.TRUE);
                        } else {
                            context.removeAttribute(REQ_ATTR_USING_SESSIONID_HEADER);
                        }
                        return shouldRetry;
                    }

                    @Override
                    public long getRetryInterval() {
                        return 0;
                    }
                })
                .addInterceptorFirst(new HttpRequestInterceptor() {

                    @Override
                    public void process(HttpRequest request, HttpContext context)
                            throws HttpException, IOException {
                        Boolean withSessionIdHeader = (Boolean) context
                                .getAttribute(REQ_ATTR_USING_SESSIONID_HEADER);
                        if (withSessionIdHeader == null || !withSessionIdHeader) {
                            if (_logger.isDebugEnabled()) {
                                _logger.debug("Removing vmware-api-session-id header");
                            }
                            request.removeHeaders(ServiceProxyController.SESSION_ID_HEADER_NAME);
                        }
                    }
                })

例外是:

Caused by: org.apache.http.client.NonRepeatableRequestException: Cannot retry request with a non-repeatable request entity
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:108)
    at org.apache.http.impl.execchain.ServiceUnavailableRetryExec.execute(ServiceUnavailableRetryExec.java:85)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    ... 51 common frames omitted

我试图修改原始请求以将其标记为未使用,但是似乎不允许...

我想知道是否还有其他方法可以解决此问题? 预先感谢。

0 个答案:

没有答案