http接口自动化测试

基于testNG的测试

1、Http工作原理
HTTP协议定义Web客户端如何从Web服务器请求Web页面,以及服务器如何把Web页面传送给客户端。HTTP协议采用了请求/响应模型。客户端向服务器发送一个请求报文,请求报文包含请求的方法、URL、协议版本、请求头部和请求数据。服务器以一个状态行作为响应,响应的内容包括协议的版本、成功或者错误代码、服务器信息、响应头部和响应数据。

2、HTTP 请求/响应的步骤:
客户端连接到Web服务器->发送Http请求->服务器接受请求并返回HTTP响应->释放连接TCP连接->客户端浏览器解析HTML内容

客户端连接到Web服务器

一个HTTP客户端,通常是浏览器,与Web服务器的HTTP端口(默认为80)建立一个TCP套接字连接。例如,http://www.baidu.com

发送Http请求

通过TCP套接字,客户端向Web服务器发送一个文本的请求报文,一个请求报文由请求行、请求头部、空行和请求数据4部分组成。

服务器接受请求并返回HTTP响应

Web服务器解析请求,定位请求资源。服务器将资源复本写到TCP套接字,由客户端读取。一个响应由状态行、响应头部、空行和响应数据4部分组成。

释放连接TCP连接

若connection 模式为close,则服务器主动关闭TCP连接,客户端被动关闭连接,释放TCP连接;若connection 模式为keepalive,则该连接会保持一段时间,在该时间内可以继续接收请求;

客户端浏览器解析HTML内容

客户端浏览器首先解析状态行,查看表明请求是否成功的状态代码。然后解析每一个响应头,响应头告知以下为若干字节的HTML文档和文档的字符集。客户端浏览器读取响应数据HTML,根据HTML的语法对其进行格式化,并在浏览器窗口中显示。
————————————————

1.http接口请求封装,发送GET、POST请求

httpclient的pom依赖

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.5</version>
        </dependency>

封装的方法

实际中,有需要登录态访问的接口,有不需要登录态就可以访问。

a.登录态的,可以通过登录接口获取到token(此处可以封装1个获取登录态的方法)

b.获取到token后,再去请求要测试的接口

发送http请求代码块

public class HttpClientUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientUtil.class);
    private static final int SUCCESS_CODE = 200;
    private static final int REDIRECT_CODE = 302;
    private static final int SUCCESS_CODE_2 = 201;

    public HttpClientUtil() {
    }

    public static JSONArray sendGet(String url) throws Exception {
        JSONArray jsonObject = null;
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;

        try {
            client = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(url);
            response = client.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            if (200 == statusCode) {
                HttpEntity entity = response.getEntity();
                String result = EntityUtils.toString(entity, "UTF-8");

                try {
                    jsonObject = JSONObject.parseArray(result);
                    JSONArray var8 = jsonObject;
                    return var8;
                } catch (Exception var14) {
                    Object var9 = null;
                    return (JSONArray)var9;
                }
            }

            LOGGER.error("HttpClientService-line: {}, errorMsg{}", 97, "GET请求失败!");
        } catch (Exception var15) {
            LOGGER.error("HttpClientService-line: {}, Exception: {}", 100, var15);
        } finally {
            if (response != null) {
                response.close();
            }

            client.close();
        }

        return null;
    }

    public static String sendPostPairToken(String url, Map<String, String> otherParams, String token, String tokenName) throws Exception {
        String result = "";
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;

        try {
            List<NameValuePair> params = new ArrayList();
            Iterator var8 = otherParams.entrySet().iterator();

            while(var8.hasNext()) {
                Map.Entry<String, String> map = (Map.Entry)var8.next();
                params.add(new BasicNameValuePair((String)map.getKey(), (String)map.getValue()));
            }

            if (otherParams != null && otherParams.size() > 0) {
                url = url + "?";

                String key;
                for(var8 = otherParams.keySet().iterator(); var8.hasNext(); url = url + key + "=" + (String)otherParams.get(key) + "&") {
                    key = (String)var8.next();
                }

                url = url.substring(0, url.length() - 1);
            }

            HttpPost post = new HttpPost(url);
            post.setEntity(new UrlEncodedFormEntity(params));
            post.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded"));
            post.setHeader(new BasicHeader(tokenName, token));
            CookieStore cookieStore = new BasicCookieStore();
            BasicClientCookie cookie = new BasicClientCookie("sid", token);
            cookie.setVersion(0);
            cookie.setDomain("10.0.91.111");
            cookie.setPath("/");
            cookie.setAttribute("path", "/");
            cookie.setAttribute("domain", "10.0.91.111");
            cookieStore.addCookie(cookie);
            client = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
            response = client.execute(post);
            if (response != null && response.getStatusLine() != null) {
                int statusCode = response.getStatusLine().getStatusCode();
                if (200 == statusCode) {
                    result = EntityUtils.toString(response.getEntity(), "UTF-8");
                    String var12 = result;
                    return var12;
                }

                LOGGER.error("HttpClientService-line: {}, errorMsg:{}", 146, "POST请求失败!");
            }
        } catch (Exception var16) {
            LOGGER.error("HttpClientService-line: {}, Exception:{}", 149, var16);
        } finally {
            if (response != null) {
                response.close();
            }

            client.close();
        }

        return result;
    }

    public static JSONObject sendPostBodyToken(String url, String bodyData, String token) throws Exception {
        JSONObject jsonObject = null;
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;

        try {
            client = HttpClients.createDefault();
            HttpPost post = new HttpPost(url);
            StringEntity entity = new StringEntity(bodyData, "UTF-8");
            post.setEntity(entity);
            post.setHeader(new BasicHeader("Content-Type", "application/json"));
            post.setHeader(new BasicHeader("login-token", token));
            response = client.execute(post);
            if (response != null && response.getStatusLine() != null) {
                int statusCode = response.getStatusLine().getStatusCode();
                if (200 == statusCode) {
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8");

                    try {
                        jsonObject = JSONObject.parseObject(result);
                        JSONObject var10 = jsonObject;
                        return var10;
                    } catch (Exception var16) {
                        Object var11 = jsonObject;
                        return (JSONObject)var11;
                    }
                }

                System.out.println(EntityUtils.toString(response.getEntity(), "UTF-8") + "POST请求失败!");
            }
        } catch (Exception var17) {
            LOGGER.error("HttpClientService-line: {}, Exception:{}", 149, var17);
        } finally {
            if (response != null) {
                response.close();
            }

            client.close();
        }

        return null;
    }

    public static JSONObject sendPostBodyToken(String url, String bodyData, String token, String tokenName) throws Exception {
        JSONObject jsonObject = null;
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;

        try {
            client = HttpClients.createDefault();
            HttpPost post = new HttpPost(url);
            StringEntity entity = new StringEntity(bodyData, "UTF-8");
            post.setEntity(entity);
            post.setHeader(new BasicHeader("Content-Type", "application/json"));
            post.setHeader(new BasicHeader(tokenName, token));
            response = client.execute(post);
            if (response != null && response.getStatusLine() != null) {
                int statusCode = response.getStatusLine().getStatusCode();
                if (200 == statusCode) {
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8");

                    try {
                        jsonObject = JSONObject.parseObject(result);
                        JSONObject var11 = jsonObject;
                        return var11;
                    } catch (Exception var17) {
                        Object var12 = jsonObject;
                        return (JSONObject)var12;
                    }
                }

                LOGGER.error("HttpClientService-line: {}, errorMsg:{}", 146, "POST请求失败!");
            }
        } catch (Exception var18) {
            LOGGER.error("HttpClientService-line: {}, Exception:{}", 149, var18);
        } finally {
            if (response != null) {
                response.close();
            }

            client.close();
        }

        return null;
    }

    public static String sendPutBodyToken(String url, String bodyData, String token, String tokenName) throws Exception {
        JSONObject jsonObject = null;
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;

        String var10;
        try {
            client = HttpClients.createDefault();
            HttpPut post = new HttpPut(url);
            if (bodyData != null) {
                StringEntity entity = new StringEntity(bodyData, "UTF-8");
                post.setEntity(entity);
            }

            post.setHeader(new BasicHeader("Content-Type", "application/json"));
            post.setHeader(new BasicHeader(tokenName, token));
            response = client.execute(post);
            if (response == null || response.getStatusLine() == null) {
                return null;
            }

            int statusCode = response.getStatusLine().getStatusCode();
            if (200 != statusCode) {
                LOGGER.error("HttpClientService-line: {}, errorMsg:{}", 146, "POST请求失败!");
                return null;
            }

            String result = EntityUtils.toString(response.getEntity(), "UTF-8");
            var10 = result;
        } catch (Exception var14) {
            LOGGER.error("HttpClientService-line: {}, Exception:{}", 149, var14);
            return null;
        } finally {
            if (response != null) {
                response.close();
            }

            client.close();
        }

        return var10;
    }

    public static JSONObject sendPutBodyToken(String url, String bodyData, String token) throws Exception {
        JSONObject jsonObject = null;
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;

        try {
            client = HttpClients.createDefault();
            HttpPut post = new HttpPut(url);
            if (bodyData != null) {
                StringEntity entity = new StringEntity(bodyData, "UTF-8");
                post.setEntity(entity);
            }

            post.setHeader(new BasicHeader("Content-Type", "application/json"));
            post.setHeader(new BasicHeader("login-token", token));
            response = client.execute(post);
            if (response != null && response.getStatusLine() != null) {
                int statusCode = response.getStatusLine().getStatusCode();
                if (200 == statusCode) {
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8");

                    try {
                        jsonObject = JSONObject.parseObject(result);
                        JSONObject var9 = jsonObject;
                        return var9;
                    } catch (Exception var15) {
                        Object var10 = jsonObject;
                        return (JSONObject)var10;
                    }
                }

                LOGGER.error("HttpClientService-line: {}, errorMsg:{}", 146, "POST请求失败!");
            }
        } catch (Exception var16) {
            LOGGER.error("HttpClientService-line: {}, Exception:{}", 149, var16);
        } finally {
            if (response != null) {
                response.close();
            }

            client.close();
        }

        return null;
    }

    public static JSONObject sendPostBodyToken2(String url, String bodyData, String token) throws Exception {
        JSONObject jsonObject = null;
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;

        try {
            client = HttpClients.createDefault();
            HttpPost post = new HttpPost(url);
            StringEntity entity = new StringEntity(bodyData, "UTF-8");
            post.setEntity(entity);
            post.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"));
            if (!token.equals("")) {
                post.setHeader(new BasicHeader("login-token", token));
            }

            response = client.execute(post);
            if (response != null && response.getStatusLine() != null) {
                int statusCode = response.getStatusLine().getStatusCode();
                System.out.println("statusCode" + statusCode);
                if (201 == statusCode) {
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8");
                    System.out.println("result:" + result);

                    try {
                        jsonObject = JSONObject.parseObject(result);
                        JSONObject var10 = jsonObject;
                        return var10;
                    } catch (Exception var16) {
                        var16.printStackTrace();
                        Object var11 = jsonObject;
                        return (JSONObject)var11;
                    }
                }

                LOGGER.error("HttpClientService-line: {}, errorMsg:{}", 146, "POST请求失败!");
            }
        } catch (Exception var17) {
            LOGGER.error("HttpClientService-line: {}, Exception:{}", 149, var17);
        } finally {
            if (response != null) {
                response.close();
            }

            client.close();
        }

        return null;
    }

    public static String sendPostGetToken(String url, String bodyData, String headName) throws Exception {
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;

        String var9;
        try {
            client = HttpClients.createDefault();
            HttpPost post = new HttpPost(url);
            StringEntity entity = new StringEntity(bodyData, "UTF-8");
            post.setEntity(entity);
            post.setHeader(new BasicHeader("Content-Type", "application/json"));
            response = client.execute(post);
            int statusCode = response.getStatusLine().getStatusCode();
            if (200 != statusCode) {
                LOGGER.error("HttpClientService-line: {}, errorMsg:{}", 146, "POST请求失败!");
                return "";
            }

            String result = response.getHeaders(headName)[0].getValue();
            var9 = result;
        } catch (Exception var13) {
            LOGGER.error("HttpClientService-line: {}, Exception:{}", 149, var13);
            return "";
        } finally {
            if (response != null) {
                response.close();
            }

            client.close();
        }

        return var9;
    }

    public static List<String> sendGetCookie(String url, List<NameValuePair> nameValuePairList) throws Exception {
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;

        try {
            client = HttpClients.createDefault();
            URIBuilder uriBuilder = new URIBuilder(url);
            if (nameValuePairList != null) {
                uriBuilder.addParameters(nameValuePairList);
            }

            HttpGet httpGet = new HttpGet(uriBuilder.build());
            response = client.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            if (200 == statusCode || 302 == statusCode) {
                HttpEntity entity = response.getEntity();
                String result = EntityUtils.toString(entity, "UTF-8");
                List<String> results = new ArrayList();
                results.add(result);
                Header[] cookies = response.getHeaders("Set-Cookie");
                Header[] var11 = cookies;
                int var12 = cookies.length;

                for(int var13 = 0; var13 < var12; ++var13) {
                    Header c = var11[var13];
                    results.add(c.getValue().split(";")[0]);
                }

                ArrayList var20 = (ArrayList) results;
                return var20;
            }

            LOGGER.error("HttpClientService-line: {}, errorMsg{}", 97, "GET请求失败!");
        } catch (Exception var18) {
            LOGGER.error("HttpClientService-line: {}, Exception: {}", 100, var18);
        } finally {
            if (response != null) {
                response.close();
            }

            client.close();
        }

        return null;
    }

    public static JSONObject sendPostByCookie(String url, List<NameValuePair> nameValuePairList, List<String> cookies) throws Exception {
        JSONObject jsonObject = null;
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;

        try {
            HttpPost post = new HttpPost(url);
            post.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded"));
            String result;
            if (cookies != null) {
                String cookie = "";

                for(Iterator var8 = cookies.iterator(); var8.hasNext(); cookie = cookie + result + ";") {
                    result = (String)var8.next();
                }

                post.setHeader(new BasicHeader("Cookie", cookie));
            }

            client = HttpClients.createDefault();
            post.setEntity(new UrlEncodedFormEntity(nameValuePairList, Consts.UTF_8));
            response = client.execute(post);
            int statusCode = response.getStatusLine().getStatusCode();
            if (200 == statusCode) {
                HttpEntity entity = response.getEntity();
                result = EntityUtils.toString(entity, "UTF-8");

                try {
                    jsonObject = JSONObject.parseObject(result);
                    JSONObject var10 = jsonObject;
                    return var10;
                } catch (Exception var16) {
                    Object var11 = jsonObject;
                    return (JSONObject)var11;
                }
            }

            LOGGER.error("HttpClientService-line: {}, errorMsg{}", 97, "GET请求失败!");
        } catch (Exception var17) {
            LOGGER.error("HttpClientService-line: {}, Exception: {}", 100, var17);
        } finally {
            if (response != null) {
                response.close();
            }

            client.close();
        }

        return null;
    }

    public static JSONObject sendGetPairToken(String url, String token, String tokenName) throws Exception {
        JSONObject jsonObject = new JSONObject();
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;

        try {
            client = HttpClients.createDefault();
            HttpGet put = new HttpGet(url);
            put.setHeader(new BasicHeader("Content-Type", "application/json"));
            put.setHeader(new BasicHeader(tokenName, token));
            response = client.execute(put);
            if (response != null && response.getStatusLine() != null) {
                int statusCode = response.getStatusLine().getStatusCode();
                if (200 == statusCode) {
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8");

                    try {
                        jsonObject = JSONObject.parseObject(result);
                        JSONObject var9 = jsonObject;
                        return var9;
                    } catch (Exception var15) {
                        JSONObject var10 = jsonObject;
                        return var10;
                    }
                }

                LOGGER.error("HttpClientService-line: {}, errorMsg:{}", 146, "put请求失败!");
            }
        } catch (Exception var16) {
            LOGGER.error("HttpClientService-line: {}, Exception:{}", 149, var16);
        } finally {
            if (response != null) {
                response.close();
            }

            client.close();
        }

        return jsonObject;
    }

    public static JSONObject sendGetPairToken2(String url, String token) throws Exception {
        JSONObject jsonObject = new JSONObject();
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;

        try {
            client = HttpClients.createDefault();
            HttpGet put = new HttpGet(url);
            put.setHeader(new BasicHeader("Content-Type", "application/json"));
            put.setHeader(new BasicHeader("login-token", token));
            response = client.execute(put);
            if (response != null && response.getStatusLine() != null) {
                int statusCode = response.getStatusLine().getStatusCode();
                if (200 == statusCode) {
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").replaceAll("\\$ref", "ref").replaceAll("#/definitions/", "");

                    try {
                        jsonObject = JSONObject.parseObject(result);
                        JSONObject var8 = jsonObject;
                        return var8;
                    } catch (Exception var14) {
                        JSONObject var9 = jsonObject;
                        return var9;
                    }
                }

                LOGGER.error("HttpClientService-line: {}, errorMsg:{}", 146, "put请求失败!");
            }
        } catch (Exception var15) {
            LOGGER.error("HttpClientService-line: {}, Exception:{}", 149, var15);
        } finally {
            if (response != null) {
                response.close();
            }

            client.close();
        }

        return jsonObject;
    }

    public static List<NameValuePair> getParams(Object[] params, Object[] values) {
        boolean flag = params.length > 0 && values.length > 0 && params.length == values.length;
        if (!flag) {
            LOGGER.error("HttpClientService-line: {}, errorMsg:{}", 197, "请求参数为空且参数长度不一致");
            return null;
        } else {
            List<NameValuePair> nameValuePairList = new ArrayList();

            for(int i = 0; i < params.length; ++i) {
                nameValuePairList.add(new BasicNameValuePair(params[i].toString(), values[i].toString()));
            }

            return nameValuePairList;
        }
    }

    public static CookieStore setCookies(Map<String, String> cookies) {
        CookieStore cookieStore = new BasicCookieStore();
        Iterator var2 = cookies.entrySet().iterator();

        while(var2.hasNext()) {
            Map.Entry<String, String> e = (Map.Entry)var2.next();
            List<String> values = Arrays.asList(((String)e.getValue()).split(";"));
            String cookieValue = "";
            String path = "";
            String domain = "";
            Iterator var8 = values.iterator();

            while(var8.hasNext()) {
                String value = (String)var8.next();
                if (value.indexOf((String)e.getKey()) > -1) {
                    cookieValue = value.split("=")[1];
                    System.out.println("cookieValue" + cookieValue);
                } else if (value.indexOf("Path") > -1) {
                    path = value.split("=")[1];
                    System.out.println("path" + path);
                } else if (value.lastIndexOf("Domain") > -1) {
                    domain = value.split("=")[1];
                    System.out.println("domain" + domain);
                }
            }

            BasicClientCookie cookie = new BasicClientCookie((String)e.getKey(), cookieValue);
            cookie.setPath(path);
            cookie.setDomain(domain);
            cookie.setExpiryDate(new Date(System.currentTimeMillis() + 360000000L));
            cookie.setAttribute("path", "/");
            cookieStore.addCookie(cookie);
        }

        return cookieStore;
    }
}

2.testNG的pom依赖

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.8.8</version>
</dependency>

3.简单接口示例

        a.获取登录态,调用封装好的登录态接口

        b.发送请求

        c.返回结果断言

 @Test
    public void modifyQuickReplyTest(){
        String doctorMobile = "18888888888";
        String password = "18888888888";
        String tokenName = "X-MOBILE-Token";
        UserLogin userLogin = new UserLogin();
        Random random = new Random();
        int num = random.nextInt(999999);
        String url = "https://xxxx/api/xxxx/xx/xxx";
        String body = "{\"id\":120510,\"tagId\":120220,\"content\":\"自动化编辑回复话术"+num+"\"}";
        String token = null;
        try {
            token = userLogin.getDoctorLoginToken(doctorMobile,password).getToken();
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject response = null;
        try {
            response = HttpClientUtil.sendPostBodyToken(url,body,token,tokenName);
            System.out.println(">>>>>>>>>>"+response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String baseResult = response.get("baseResult").toString();
        JSONObject isSuccess = JSON.parseObject(baseResult);
        Assert.assertTrue(isSuccess.get("isSuccess").toString().equals("true"));
    }

封装的登录获取token的方法,代码块

public class UserLogin {

    public UserToken getDoctorLoginToken(String loginId,String password) throws Exception{
        JSONObject response = sendPostBodyToken("https://api.test.xxxx.cn/api/cif-login/cif/loginService/webLogin",
                "{\n" +
                        "  \"appType\": \"MANAGER_SYSTEM\",\n" +
                        "  \"autoLogin\": true,\n" +
                        "  \"userName\": 51665166516,\n" +
                        "  \"loginId\": "+loginId+",\n" +
                        "  \"password\": "+password+",\n" +
                        "  \"roleType\": \"DOCTOR\",\n" +
                        "  \"traceLogId\": \"5\",\n" +
                        "  \"type\": \"account\"\n" +
                        "}");
        String result= response.get("result").toString();
        JSONObject object = JSONObject.parseObject(result);
        String token = object.get("sessionKey").toString();
        long doctorId = Long.parseLong(object.get("operatorNo").toString());
        UserToken userToken = new UserToken();
        userToken.setToken(token);
        userToken.setDoctorId(doctorId);
        return userToken;
    }

    public UserToken getUserLoginToken(String loginId) throws Exception{
        JSONObject response = sendPostBodyToken("https://api.test.xxxx.cn/api/cif-login/cif/loginService/smsLogin",
                "{\n" +
                        "  \"traceLogId\": 2,\n" +
                        "  \"appType\": \"IOS\",\n" +
                        "  \"mobileNo\": "+loginId+",\n" +
                        "  \"verificationCode\": \"666666\",\n" +
                        "  \"verificationSeqNo\": \"SMS_A00003\",\n" +
                        "  \"protocolCode\": \"A0001\",\n" +
                        "  \"protocolVersion\": \"2.0\",\n" +
                        "  \"roleType\": \"USER\"\n" +
                        "}");
        String result= response.get("result").toString();
        JSONObject object = JSONObject.parseObject(result);
        String token = object.get("sessionKey").toString();
        long doctorId = Long.parseLong(object.get("operatorNo").toString());
        UserToken userToken = new UserToken();
        userToken.setToken(token);
        userToken.setDoctorId(doctorId);
        return userToken;
    }

    public static JSONObject sendPostBodyToken(String url, String bodyData) throws Exception {
        JSONObject jsonObject = null;
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;
        try {
            client = HttpClients.createDefault();
            HttpPost post = new HttpPost(url);
            StringEntity entity = new StringEntity(bodyData, "UTF-8");
            post.setEntity(entity);
            post.setHeader(new BasicHeader("Content-Type", "application/json"));
            response = client.execute(post);
            if (response != null && response.getStatusLine() != null) {
                int statusCode = response.getStatusLine().getStatusCode();
                if (200 == statusCode) {
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8");
                    try {
                        jsonObject = JSONObject.parseObject(result);
                        JSONObject var11 = jsonObject;
                        return var11;
                    } catch (Exception var17) {
                        JSONObject var12 = (JSONObject)jsonObject;
                        return var12;
                    }
                }
                System.out.println(EntityUtils.toString(response.getEntity(), "UTF-8") + "POST请求失败!");
            }
        } catch (Exception var18) {
        } finally {
            if (response != null) {
                response.close();
            }
            client.close();
        }
        return null;
    }

    public class UserToken{
        private String token;
        private long doctorId;

        public long getDoctorId() {
            return doctorId;
        }

        public void setDoctorId(long doctorId) {
            this.doctorId = doctorId;
        }

        public void setToken(String token) {
            this.token = token;
        }

        public String getToken() {
            return token;
        }
    }
}

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值