HTTP GET添加Header请求头

package com.simpro.util;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
/**
 * 
 * @author Wangzhao
 *
 * 2020年2月19日-上午11:26:33
 *   人脸识别-GET 请求(增加Header请求头)
 */
public class QdfsFaceToGetHttpHeader {
  public static void main(String[] args) {
    String url = "http://123.127.2.222:22111/recognizeRecords/top";
    String page = "1";
    String size = "6";
    String token ="a058257fc2fb47b9a6acc43d6a879f49";
    getParamss(url,page,size,token);
  }
  
  public static JSONObject  getParamss(String url,String page,String size,String token) {
   
      // 获取连接客户端工具
      CloseableHttpClient httpClient = HttpClients.createDefault();
   
      String entityStr = null;
      CloseableHttpResponse response = null;
   
      try {
          /*
           * 由于GET请求的参数都是拼装在URL地址后方,所以我们要构建一个URL,带参数
           */
          URIBuilder uriBuilder = new URIBuilder(url);
          /** 第一种添加参数的形式 */
          /*uriBuilder.addParameter("name", "root");
          uriBuilder.addParameter("password", "123456");*/
          /** 第二种添加参数的形式 */
          List<NameValuePair> list = new LinkedList<>();
          BasicNameValuePair param1 = new BasicNameValuePair("page", page);
          BasicNameValuePair param2 = new BasicNameValuePair("size", size);
          list.add(param1);
          list.add(param2);
          uriBuilder.setParameters(list);
   
          // 根据带参数的URI对象构建GET请求对象
          HttpGet httpGet = new HttpGet(uriBuilder.build());
   
          /* 
           * 添加请求头信息
           */
          // 浏览器表示
          httpGet.addHeader("token", token);
          // 传输的类型
          httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded");
   
          // 执行请求
          response = httpClient.execute(httpGet);
          // 获得响应的实体对象
          HttpEntity entity = response.getEntity();
          // 使用Apache提供的工具类进行转换成字符串
          entityStr = EntityUtils.toString(entity, "UTF-8");
      } catch (ClientProtocolException e) {
          System.err.println("Http协议出现问题");
          e.printStackTrace();
      } catch (ParseException e) {
          System.err.println("解析错误");
          e.printStackTrace();
      } catch (URISyntaxException e) {
          System.err.println("URI解析异常");
          e.printStackTrace();
      } catch (IOException e) {
          System.err.println("IO异常");
          e.printStackTrace();
      } finally {
          // 释放连接
          if (null != response) {
              try {
                  response.close();
                  httpClient.close();
              } catch (IOException e) {
                  System.err.println("释放连接出错");
                  e.printStackTrace();
              }
          }
      }
   
      // 打印响应内容
      System.out.println(entityStr);
      return new JSONObject(entityStr);
   
  }
}

 

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值