栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

HttpClient的POST请求返回302解决

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

HttpClient的POST请求返回302解决

HttpClient请求POST提示302,而且返回的response中的Localtion是我访问时使用的URL,
例如:我使用的URL是https://bbs.csdn.net?client_id=10333&user=test,
localtion中也是这个https://bbs.csdn.net?client_id=10333&user=test

我试了直接使用curl可以返回json
我的post方法:

public static StringBuilder post(String url, Object data, String encoding) {
 log.info("HttpClient post start => "+url);
 CloseableHttpResponse response = null;
 HttpPost httpPost = null;
 HttpHost httpHost = null;
 StringBuilder responseEntity = null;
 String strings = "";

     URL _url = new URL(url);

     httpPost = new HttpPost(url);

     httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");

     if(!Stringer.isNullOrEmpty(data)){

  log.debug("HttpClient post url => "+url+", data:"+JSON.toJSONString(data));

  if (data instanceof Map) {
      // Map方式传参处理
      Map params = (Map) data;

      List nvps = new ArrayList();
      if (!Stringer.isNullOrEmpty(data)) {
   Set keySet = params.keySet();
   for (String key : keySet) {
nvps.add(new BasicNamevaluePair(key, params.get(key)));
   }
      }
      httpPost.setEntity(new UrlEncodedFormEntity(nvps, encoding));

  } else if(data instanceof String){
      // String方式传参处理
      strings = (String)data;
      strings = Stringer.nullToEmpty(strings);
      ByteArrayEntity reqEntity = new ByteArrayEntity(strings.getBytes(encoding));
      reqEntity.setContentEncoding(encoding);
      httpPost.setEntity(reqEntity);
  }
     }

     httpPost.setConfig(defaultRequestConfig);

     httpHost = new HttpHost(_url.getHost(),_url.getPort());  
     response = httpClient.execute(httpHost,httpPost);

     if(!Stringer.isNullOrEmpty(response)){
  log.info("2--HttpClient response  => "+response);
  log.info("3--HttpClient StatusCode  => "+response.getStatusLine().getStatusCode());
  
  String locationUrl=response.getLastHeader("location").getValue();
  log.info("HttpClient locationUrl  => "+locationUrl);
  log.info("4--HttpClient StatusCode  => "+response.getStatusLine().getStatusCode());

  if (response.getStatusLine().getStatusCode() == 302) {
      Header header = response.getFirstHeader("location"); // 跳转的目标地址是在 HTTP-HEAD上
      String newuri = header.getValue(); // 这就是跳转后的地址,再向这个地址发出新申请
      System.out.println(newuri);

      httpPost = new HttpPost(newuri);
      httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");


      response = httpClient.execute(httpHost,httpPost);
      int code = response.getStatusLine().getStatusCode();
      locationUrl=response.getLastHeader("location").getValue();
      log.info("5--HttpClient new Code  => "+code);
      log.info("6--HttpClient locationUrl  => "+locationUrl);
  }
  responseEntity = new StringBuilder();
  responseEntity.append(EntityUtils.toString(response.getEntity(), encoding));
  return responseEntity;
     }

 return null;
    }

curl命令样例:

curl -k -d “client_id=10333&user=test” “https://bbs.csdn.net”

原因:
官方文档中对HttpHost(String hostname,int port)方法的说明:

HttpHost
public HttpHost(String hostname,
int port)
Constructor for HttpHost.
Parameters:
hostname - the hostname (IP or DNS name). Can be null.
port - the port. Value -1 can be used to set default protocol port

可见 如果不在方法里面增加协议会带上默认协议http
需要使用带协议的方法:HttpHost(String hostname, int port, Protocol protocol) ,并指定协议为https

httpHost = new HttpHost(_url.getHost(),_url.getPort(),“https”);

至此问题解决!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/237415.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号