接口自动化之封装httpclientUtil类

package com.second;

import jdk.internal.dynalink.beans.StaticClass;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.HeadMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import sun.invoke.empty.Empty;


import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by Administrator on 2017/8/12 0012.
 */
public class HttpClientUtil {
    //返回两个结果 code和返回结果
    public static Map<String,String> request(Map<String,String>baseParam,Map<String,String>headerMap) throws IOException {
        HttpClient httpClient = new HttpClient();
        Map<String,String> returnMap = new HashMap<String, String>();
        //判断url为空和不为空的情况
        String url = (String)baseParam.get("url");
   //     returnMap.put("status","500");
        if(isEmpty(url)){
            returnMap.put("returnBody","url不能为空");
            return returnMap;
        }
        //判断methodName 为空和不为空的情况
        String methodName = (String)baseParam.get("methodName");
        if(isEmpty(methodName)){
            returnMap.put("returnBody","方法不能为空");
            return returnMap;
        }
        // 下面的编码格式 超时 ip 端口号这些如果是空,就直接给一个值了。url和method这两个是必须不能为空的。
        String encoding = (String)baseParam.get("encoding");
        String connectionTimeout = (String)baseParam.get("connectionTimeout");
        String soTimeout = (String)baseParam.get("connectionTimeout");
        String ip = (String)baseParam.get("ip");
        String portStr = (String) baseParam.get("portStr");
        String body = (String) baseParam.get("body");
        HttpMethodBase method = null;

        //获取方法
        if(methodName.equalsIgnoreCase("post")){
            PostMethod method1 = new PostMethod(url);
            if(!isEmpty(body)){
                method1.setRequestBody(body);
            }
            method = method1;
        }else {
           method = new GetMethod(url);
        }

        //动态代理host
        int port = 80;
        //ip如果不为空,说明要走代理
        if(!isEmpty(ip)){
            //端口号也判断一下,如果端口为空,我们默认给他80端口
            if(isEmpty(portStr)){
                httpClient.getHostConfiguration().setProxy(ip,port);
            }//如果端口号不为空,把portStr 传进来
            else{
                httpClient.getHostConfiguration().setProxy(ip,Integer.parseInt(portStr));
            }

        }
        //设置编码格式
        HttpClientParams params = httpClient.getParams();
        //if 中为真执行第一个大括号里的,为假执行else大括号里面的,为空设置编码格式
        if(isEmpty(encoding)){
            params.setContentCharset("UTF-8");
        } //不为空的时候把encoding传进来
        else{
            params.setContentCharset("encoding");
        }


        //设置超时
        HttpConnectionManagerParams params1 = httpClient.getHttpConnectionManager().getParams();
        if(isEmpty(connectionTimeout)){
            //等于空的时候默认给他一个5秒
            params1.setConnectionTimeout(5000);

        }else{
            //不为空的时候把connectionTimeout 传进来
            params1.setConnectionTimeout(Integer.parseInt(connectionTimeout));
        }

        if(isEmpty(soTimeout)){
            params1.setSoTimeout(1000*60);
        } //不为空的时候把soTimeout 传进来
        else {
            params1.setSoTimeout(Integer.parseInt(soTimeout));
        }

        //设置消息头,先判断header是否为空,然后遍历header
        if(null != headerMap){
            for(String key : headerMap.keySet()){
                method.setRequestHeader(key,headerMap.get(key));
            }
        }
        int status = 0;
        String returnBody = "";
        status = httpClient.executeMethod(method);
        returnBody = method.getResponseBodyAsString();

        Map<Integer,String> codeMap = new HashMap<Integer, String>();
        {
            codeMap.put(401,"执行方法没有授权");
            codeMap.put(405,"执行方法不对,请确认是get或是post");
            codeMap.put(415,"请确认content-type类型是否正确");
        }


        if(status!=200){
            String exceptionMsg=codeMap.get(status);
            if(isEmpty(exceptionMsg)){
                returnMap.put("returnBody","有异常错误,百度查一下code码含义");
            }else{
                returnMap.put("returnBody",codeMap.get(status));
            }
            returnMap.put("status",status+"");


        }else {
            returnMap.put("status",status+"");
            returnMap.put("returnBody",returnBody);
        }

        return returnMap;


    }

    private static boolean isEmpty(String n){
        boolean f = true;
        if(null!=n&&!"".equals(n)){
            f = false;
        }
        return f;
    }
}

新建HttpClientUtilTestPractice 类 用@test 调用HttpClientUtilPractice 封装好的类

public class HttpClientUtilTestPractice {

    @Test
   public void getUserByIdTest1() throws IOException {
        Map<String, String> baseParam = new HashMap<String, String>();
        String url = "http://localhost:8881/DemoController/getUserById?id=1";
        baseParam.put("url",url);
        String methodName = "get";
        baseParam.put("methodName",methodName);
        Map<String,String> returnMap = HttpClientUtilPractice.request(baseParam,null);
        System.out.print("返回状态码是:"+returnMap.get("status")+";返回结果是:"+returnMap.get("returnBody"));
    }
    @Test
    public void postMethodTest() throws IOException {
        Map<String,String > baseParam = new HashMap<String, String>();
        String url = "http://localhost:8881/DemoController/modifyUserByIdBody/1";
        String body = "{\"name\":\"longtest\",\"email\":\"longteng@1111.com\"}";
        String methodName = "post";
        baseParam.put("url",url);
        baseParam.put("body",body);
        baseParam.put("methodName",methodName);
        Map<String,String> returnMap = HttpClientUtil.request(baseParam,null);
        System.out.print("返回状态码:"+returnMap.get("status")+";返回结果是:"+returnMap.get("returnBody"));
    }


}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
、 开发环境................................................................................................................................................................... 1 2、 大致流程................................................................................................................................................................... 2 3、 框架简介................................................................................................................................................................... 2 4、 运行结果展示 ........................................................................................................................................................... 3 5、 文件与配置............................................................................................................................................................... 3 6、 测试接口样例 ........................................................................................................................................................... 4  1.登陆接口................................................................................................................................................................... 4  2.支付密码更改接口 ................................................................................................................................................... 6 7、 数据库设计............................................................................................................................................................... 7 8、 测试用例、测试数据准备 ....................................................................................................................................... 8 9、 模块与、函数设计 ............................................................................................................................................. 10 10、 代码实现............................................................................................................................................................. 10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NeilNiu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值