Httpclient 4, error 302. How to redirect?

前端 未结 7 1935
忘了有多久
忘了有多久 2020-11-27 03:03

I want to access one site that first requires an (tomcat server) authentication and then log in with a POST request and keep that user to see the site\'s pages. I use Httpcl

相关标签:
7条回答
  • 2020-11-27 04:03

    For 4.1 version:

    DefaultHttpClient  httpclient = new DefaultHttpClient();
        httpclient.setRedirectStrategy(new DefaultRedirectStrategy() {                
            public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context)  {
                boolean isRedirect=false;
                try {
                    isRedirect = super.isRedirected(request, response, context);
                } catch (ProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (!isRedirect) {
                    int responseCode = response.getStatusLine().getStatusCode();
                    if (responseCode == 301 || responseCode == 302) {
                        return true;
                    }
                }
                return isRedirect;
            }
        });
    
    0 讨论(0)
提交回复
热议问题