Httpclient 4, error 302. How to redirect?

前端 未结 7 2030
忘了有多久
忘了有多久 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: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.
    

提交回复
热议问题