2017-02-13 61 views
0

我們的簽名算法更新HttpRequest s通過更改目標請求的主機 。如何在Apache HttpClient中更新請求的目標主機?

但是,準備給定發送請求的org.apache.http.impl.execchain.ProtocolExec#execute在任何運行實例之前計算目標主機。因此,即使HttpRequestInterceptor更新了請求的目標主機,該請求也會發送到原始主機。

如何更新請求目標主機以便將請求發送到更新的主機?

我正在使用httpclient 4.5.2。

回答

0

嘗試裝飾協議執行並使其向下執行流水線

CloseableHttpClient client = new HttpClientBuilder() { 

    @Override 
    protected ClientExecChain decorateProtocolExec(final ClientExecChain protocolExec) { 
     return new ClientExecChain() { 
      @Override 
      public CloseableHttpResponse execute(
        final HttpRoute route, 
        final HttpRequestWrapper request, 
        final HttpClientContext clientContext, 
        final HttpExecutionAware execAware) throws IOException, HttpException { 

       HttpUriRequest newRequest = RequestBuilder.copy(request.getOriginal()) 
         .setUri("http://some-place-esle.com/") 
         .build(); 

       return protocolExec.execute(route, HttpRequestWrapper.wrap(newRequest), clientContext, execAware); 
      } 
     }; 
    } 
}.build(); 
事先重寫原點請求
相關問題