httpclient 302 重定向

  1. 主要是由于页面可能不是通过 request.sendRedirect跳转的,可能通过js  location跳转的。所以需要拿跳转后的 地址,重新发送请求。。。如下所示
  2. if (status == HttpStatus.SC_MOVED_PERMANENTLY || status == HttpStatus.SC_MOVED_TEMPORARILY) {
     Header locationHeader = hm.getResponseHeader("location");
     String location = null;
     if (locationHeader != null) {
     location = locationHeader.getValue();
     GetMethod redirect=new GetMethod(location); 

    HttpClient client = new HttpClient();
    client.executeMethod(redirect);
    if (status_ != HttpStatus.SC_OK) { throw new JobExecutionException("[执行结果=失败][状态码=" + status + "]" + (hm.getStatusLine() == null ? "" : "[信息=" + hm.getStatusText() + "]")); } //responseBody = getPostResponse(location, parts);// 用跳转后的页面重新请求。  } } else if (status == HttpStatus.SC_OK) { responseBody = hm.getResponseBodyAsString(); }else{ //非正常执行,异常处理代码。。 }
原文地址:https://www.cnblogs.com/Struts-pring/p/4301443.html