展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

测试

  • 发送post请求,并使用Authorization Basic + application/json
    @GetMapping("/test4")
    @ResponseBody
    public String test4() throws IOException {
        String url ="http://192.168.96.96:8081/api/v4/auth_username";
        Map<String, String> prarms = new HashMap<>();
        prarms.put("username","user2");
        prarms.put("password", "123456");
        String jsonPrarms = JSON.toJSONString(prarms);
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();   //获取浏览器信息
        HttpPost httpPost = new HttpPost(url);
        String encoding = DatatypeConverter.printBase64Binary("admin:public".getBytes("UTF-8"));  //username  password 自行修改  中间":"不可少
        httpPost.setHeader("Authorization", "Basic " + encoding);
        HttpEntity entityParam = new StringEntity(jsonPrarms, ContentType.create("application/json", "UTF-8"));  //这里的“application/json” 可以更换因为本人是传的json参数所以用的这个
        httpPost.setEntity(entityParam);     //把参数添加到post请求
        HttpResponse response = httpClient.execute(httpPost);
        StatusLine statusLine = response.getStatusLine();   //获取请求对象中的响应行对象
        int responseCode = statusLine.getStatusCode();
        if (responseCode == 200) {
            //获取响应信息
            HttpEntity entity = response.getEntity();
            InputStream input = entity.getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(input,"utf-8"));
            String str1 = br.readLine();
            System.out.println("服务器的响应是:" + str1);
            br.close();
            input.close();
        } else {
            System.out.println("响应失败");
        }
        // end
        return "success";
    }

# 等同于如下:
@hostname = 192.168.96.96
@port=8081
@contentType=application/json
@userName=admin
@password=public

POST http://{{hostname}}:{{port}}/api/v4/auth_username HTTP/1.1
Content-Type: {{contentType}}
Authorization: Basic {{userName}}:{{password}}

{
    "username": "user1",
    "password": "123456"
}
    @GetMapping("/test5")
    @ResponseBody
    public String test5() throws IOException {
        String url ="http://192.168.96.96:8081/api/v4/auth_username/user1";
        String encoding = DatatypeConverter.printBase64Binary("admin:public".getBytes("UTF-8"));  //username  password 自行修改  中间":"不可少
        doDelete(url, encoding, "");
        // end
        return "success";
    }


    /***
     * http client发送delete请求
     * 参考:https://www.cnblogs.com/htyjlitm/p/10036292.html
     * @param url
     * @param jsonStr
     * @return
     */
    public static String doDelete(String url, String encoding, String jsonStr) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpDelete httpDelete = new HttpDelete(url);
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
        httpDelete.setConfig(requestConfig);
        httpDelete.setHeader("Content-type", "application/json");
        httpDelete.setHeader("DataEncoding", "UTF-8");
        httpDelete.setHeader("Authorization", "Basic " + encoding);
        CloseableHttpResponse httpResponse = null;
        try {
            httpResponse = httpClient.execute(httpDelete);
            HttpEntity entity = httpResponse.getEntity();
            String result = EntityUtils.toString(entity);
            return result;
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (httpResponse != null) {
                try {
                    httpResponse.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (null != httpClient) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
posted @ 2022-08-28 19:54  DogLeftover  阅读(5)  评论(0编辑  收藏  举报