zoukankan      html  css  js  c++  java
  • HttpClient4.3.3 禁止自动重定向

    HttpClient4.3中默认允许自动重定向,导致程序中不能跟踪跳转情况,其实只需要在RequestConfig中setRedirectsEnabled(false)即可(默认是true):

    private RequestConfig createConfig(int timeout, boolean redirectsEnabled)
    {
        retun RequestConfig.custom()
            .setSocketTimeout(timeout)
            .setConnectTimeout(timeout)
            .setConnectionRequestTimeout(timeout)
            .setRedirectsEnabled(redirectsEnabled)
            .build();
    }
    public void test(String url)
    {
      CloseableHttpClient client = HttpClients.createDefault();   
        try  
        {    
            HttpGet httpGet = new HttpGet(url);    
            httpGet.setConfig(createConfig(5000, false));    
            CloseableHttpResponse response = client.execute(httpGet);    
            try    
            {      
                Header h = response.getFirstHeader("Location");      
                if(h!=null)      
                {         
                    System.out.println("重定向地址:"+h.getValue());      
                }    
            }    
            finally    
            {      
                response.close();    
            }  
        }  
        finally  
        {  
          client.close();  
        }
    }

    另外如发生重定向,response的状态码为302,不是200。

  • 相关阅读:
    HDU 1501 Zipper(DFS)
    HDU 2181 哈密顿绕行世界问题(DFS)
    HDU 1254 推箱子(BFS)
    HDU 1045 Fire Net (DFS)
    HDU 2212 DFS
    HDU 1241Oil Deposits (DFS)
    HDU 1312 Red and Black (DFS)
    HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
    HDU 1022 Train Problem I(栈)
    HDU 1008 u Calculate e
  • 原文地址:https://www.cnblogs.com/niss/p/3711901.html
Copyright © 2011-2022 走看看