Httpclient 4, error 302. How to redirect?

前端 未结 7 1934
忘了有多久
忘了有多久 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 03:40

    You have to implement custom redirection handler that will indicate that response to POST is a redirection. This can be done by overriding isRedirectRequested() method as shown below.

    DefaultHttpClient client = new DefaultHttpClient();
    client.setRedirectHandler(new DefaultRedirectHandler() {                
        @Override
        public boolean isRedirectRequested(HttpResponse response, HttpContext context) {
            boolean isRedirect = super.isRedirectRequested(response, context);
            if (!isRedirect) {
                int responseCode = response.getStatusLine().getStatusCode();
                if (responseCode == 301 || responseCode == 302) {
                    return true;
                }
            }
            return isRedirect;
        }
    });
    

    In later version of HttpClient, the class name is DefaultRedirectStrategy, but similar solution can be used there.

    0 讨论(0)
  • 2020-11-27 03:52
    httpclient.setRedirectHandler(new DefaultRedirectHandler());
    

    See HttpClient Javadoc

    0 讨论(0)
  • 2020-11-27 03:56

    In later versions of HttpCLient (4.1+), you can just do this:

    DefaultHttpClient client = new DefaultHttpClient()
    client.setRedirectStrategy(new LaxRedirectStrategy())
    

    LaxRedirectStrategy will automatically redirect HEAD, GET, and POST requests. For a stricter implementation, use DefaultRedirectStrategy.

    0 讨论(0)
  • 2020-11-27 03:58

    Redirects are not handled automatically by HttpClient 4.1 for other methods than GET and PUT.

    0 讨论(0)
  • 2020-11-27 03:58
    Extend the DefaultRedirectStrategy class and override the methods.
    @Override
        protected URI createLocationURI(String arg0) throws ProtocolException {
            // TODO Auto-generated method stub
            return super.createLocationURI(arg0);
        }
    
        @Override
        protected boolean isRedirectable(String arg0) {
            // TODO Auto-generated method stub
            return true;
        }
    
        @Override
        public URI getLocationURI(HttpRequest arg0, HttpResponse arg1,
                HttpContext arg2) throws ProtocolException {
            // TODO Auto-generated method stub
            return super.getLocationURI(arg0, arg1, arg2);
        }
    
        @Override
        public HttpUriRequest getRedirect(HttpRequest request,
                HttpResponse response, HttpContext context)
                throws ProtocolException {
              URI uri = getLocationURI(request, response, context);
              String method = request.getRequestLine().getMethod();
              if (method.equalsIgnoreCase(HttpHead.METHOD_NAME)) {
                  return new HttpHead(uri);
              } else {
                  return new HttpPost(uri);
              }
    
        }
    
        @Override
        public boolean isRedirected(HttpRequest request, HttpResponse response,
                HttpContext context) throws ProtocolException {
            // TODO Auto-generated method stub
            return super.isRedirected(request, response, context);
        }
    
    in this case isRedirectable method will always return true and getRedirect method will return post request in place of get request.
    
    0 讨论(0)
  • 2020-11-27 03:59

    For HttpClient 4.3.x :

    HttpClient httpClient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
    
    0 讨论(0)
提交回复
热议问题