HttpClient的POST請求返回302解決

  HttpClient請求POST提示302,並且返回的response中的Localtion是我訪問時使用的URL,json

  例如:我使用的URL是https://bbs.csdn.net?client_id=10333user=test,app

  localtion中也是這個https://bbs.csdn.net?client_id=10333user=testcurl

  我試了直接使用curl能夠返回jsonpost

  個人post方法:ui

  public static StringBuilder post(String url, Object data, String encoding) {url

  log.info(HttpClient post start = +url);.net

  CloseableHttpResponse response = null;debug

  HttpPost httpPost = null;code

  HttpHost httpHost = null;orm

  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方式傳參處理

  Mapparams = (Map) data;

  Listnvps = new ArrayList();

  if (!Stringer.isNullOrEmpty(data)) {

  SetkeySet = 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());

  /*if (response.getStatusLine().getStatusCode() != 200) {

  return null;

  }*/

  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=10333user=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」);

  至此問題解決!

相關文章
相關標籤/搜索