• 接口测试


     1 public class test1 {
     2 
     3     public static String send(String url, Map<String, String> map, String encoding) throws ParseException, IOException {
     4         String body = "";
     5 
     6         //创建httpcilent对象
     7         CloseableHttpClient client = HttpClients.createDefault();
     8         //创建post方式请求对象
     9         HttpPost post = new HttpPost(url);
    10 
    11         //装填参数
    12         List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    13         if (map != null) {
    14             for (Map.Entry<String, String> entry : map.entrySet()) {
    15                 nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
    16             }
    17         }
    18         //设置参数到请求中
    19         post.setEntity(new UrlEncodedFormEntity(nvps, encoding));
    20 
    21         System.out.print("请求地址:" + url + "
    ");
    22         System.out.print("请求参数:" + nvps.toString() + "
    ");
    23 
    24         //设置header信息
    25         post.setHeader("Content-type", "application/json;charset=utf-8");
    26         post.setHeader("Accept", "application/json");
    27 
    28         //执行请求操作
    29         CloseableHttpResponse response = client.execute(post);
    30 
    31         //获取结果实体
    32         HttpEntity entity = response.getEntity();
    33         if (entity != null) {
    34             //实体转换成String类型
    35             body = EntityUtils.toString(entity, encoding);
    36         }
    37         EntityUtils.consume(entity);
    38         //释放链接
    39         response.close();
    40         return body;
    41     }
    42 
    43     public static String get(String url, Map<String, String> map, String encoding) throws ParseException, IOException {
    44         String body = "";
    45         //创建httpClient对象
    46         CloseableHttpClient client = HttpClients.createDefault();
    47         //创建get请求方式
    48         HttpGet get = new HttpGet(url);
    49         //装填参数
    50         List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    51         if (map != null) {
    52             for (Map.Entry<String, String> entry : map.entrySet()) {
    53                 nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
    54             }
    55         }
    56         //设置参数到请求中
    57 //        get.setEntity(new UrlEncodedFormEntity(nvps, encoding));
    58 
    59         System.out.print("请求地址:" + url + "
    ");
    60         System.out.print("请求参数:" + nvps.toString() + "
    ");
    61 
    62         //设置header信息
    63         get.setHeader("Content-type", "application/json;charset=utf-8");
    64         get.setHeader("Accept", "application/json");
    65 
    66         //执行请求操作
    67         CloseableHttpResponse response = client.execute(get);
    68         //获取结果实体
    69         HttpEntity entity = response.getEntity();
    70         if (entity != null) {
    71             //实体转换成String类型
    72             body = EntityUtils.toString(entity, encoding);
    73         }
    74         EntityUtils.consume(entity);
    75         //释放链接
    76         response.close();
    77         return body;
    78     }
    79 
    80     public static void main(String[] args) throws ParseException, IOException {
    81         String url = "http://php.weather.sina.com.cn/iframe/index/w_cl.php";
    82         Map<String, String> map = new HashMap<String, String>();
    83         map.put("code", "js");
    84         map.put("day", "0");
    85         map.put("city", "上海");
    86         map.put("charset", "utf-8");
    87 
    88         String body = get(url, map, "utf-8");
    89         System.out.print("交易响应结果:" + body + "
    ");
    90     }
    91 }

    转载地址:https://blog.csdn.net/xiaoxian8023/article/details/49863967/

  • 相关阅读:
    简单网络问题排查记录一
    安装mysql_sniffer报错undefined reference to symbol 'pthread_setspecific@@GLIBC_2.2.5'问题
    tengine安装ngx_http_lua_module
    Django——URL详解/Django中URL是如何与urls文件匹配的
    Python学习——enumerate
    Python学习——装饰器/decorator/语法糖
    缓存优化
    页面重绘 回流及其优化
    window.getComputedStyle()方法的使用及其扩展
    java 三个循环的优缺点
  • 原文地址:https://www.cnblogs.com/zhugongmeetyou/p/9541030.html
Copyright © 2020-2023  润新知