zoukankan      html  css  js  c++  java
  • 请求的个性化配置

    1. 请求头设置

    //创建 请求
    RequestBuilder requestBuilder = RequestBuilder.get().setUri("https://www.baidu.com/");
    HttpUriRequest httpGet = requestBuilder.build();
    /**
     * 设置请求头, eg: Accept: text/plain
     */
    //单个设置
    httpGet.setHeader(HttpHeaders.ACCEPT, ContentType.TEXT_PLAIN.getMimeType());
    //多个设置
    Header acceptHeader = new BasicHeader(HttpHeaders.ACCEPT, ContentType.TEXT_PLAIN.getMimeType());
    Header contentTypeHeader = new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.getMimeType());
    httpGet.setHeaders(new Header[]{acceptHeader, contentTypeHeader});

    2. HttpGet设置请求参数

    //创建 请求
    RequestBuilder requestBuilder = RequestBuilder.get().setUri("https://www.baidu.com/");
    /**
     * GET请求的参数都是拼装在URL地址后方
     *      addParameter()
     *      addParameters() ---  List<NameValuePair> --- BasicNameValuePair
     */
    //单个单个添加
    requestBuilder.addParameter("username", "JiMu");
    requestBuilder.addParameter("password", "123456");
    
    //设置参数数组
    List<NameValuePair> paramList = new ArrayList<NameValuePair>();
    BasicNameValuePair para1 = new BasicNameValuePair("username", "JiMu");
    paramList.add(para1);
    BasicNameValuePair para2 = new BasicNameValuePair("password", "123456");
    paramList.add(para2);
    requestBuilder.addParameters(paramList.toArray(new NameValuePair[paramList.size()]));
    
    HttpUriRequest httpGet = requestBuilder.build();
    //输出请求行:https://www.baidu.com/?username=JiMu&password=123456&username=JiMu&password=123456
    System.out.println(httpGet.getRequestLine().getUri());

    3. HttpPost设置请求参数

    //创建 请求
    HttpPost httpPost = new HttpPost("https://www.baidu.com/");
    /**
     * post请求的参数是放在请求体里面的,依靠 HttpPost 实例自身的setEntity()方法来设置
     * 使用StringEntity设置 --- 可灵活设定参数格式
     */
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("username", "JIMu");
    jsonObject.put("password", "123456");
    StringEntity entityParam = new StringEntity(jsonObject.toString());
    //不设置时,默认是text/plain
    entityParam.setContentType(ContentType.APPLICATION_JSON.toString());
    httpPost.setEntity(entityParam);
    //创建 请求
    HttpPost httpPost = new HttpPost("https://www.baidu.com/");
    /**
     * 使用UrlEncodedFormEntity来构建 --- 适用于传统表单格式的参数形式
     *      UrlEncodedFormEntity entityParam  = new UrlEncodedFormEntity(List<NameValuePair>, "UTF-8");
     *      httpPost.setEntity(entityParam);
     */
    NameValuePair para1 = new BasicNameValuePair("username", "名字");
    NameValuePair para2 = new BasicNameValuePair("password", "123456");
    List<NameValuePair> paramList = new ArrayList<NameValuePair>();
    paramList.add(para1);
    paramList.add(para2);
    UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(paramList, Consts.UTF_8);
    httpPost.setEntity(entityParam);
    System.out.println("获取请求信息:" + EntityUtils.toString(httpPost.getEntity()));
  • 相关阅读:
    Atitit sql计划任务与查询优化器统计信息模块
    Atitit  数据库的事件机制触发器与定时任务attilax总结
    Atitit 图像处理知识点体系知识图谱 路线图attilax总结 v4 qcb.xlsx
    Atitit 图像处理 深刻理解梯度原理计算.v1 qc8
    Atiti 数据库系统原理 与数据库方面的书籍 attilax总结 v3 .docx
    Atitit Mysql查询优化器 存取类型 范围存取类型 索引存取类型 AND or的分析
    Atitit View事件分发机制
    Atitit 基于sql编程语言的oo面向对象大规模应用解决方案attilax总结
    Atitti 存储引擎支持的国内点与特性attilax总结
    Atitit 深入理解软件的本质 attilax总结 软件三原则"三次原则"是DRY原则和YAGNI原则的折
  • 原文地址:https://www.cnblogs.com/myitnews/p/12197193.html
Copyright © 2011-2022 走看看