java 模拟发送post请求测试

Posted Mr_伍先生

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 模拟发送post请求测试相关的知识,希望对你有一定的参考价值。

方法一: HttpClient

public void postTest(HttpServletRequest request,Integer type,String phone,String passwd,String schoolld,String agent){
        String url = "xxxxxxxxx";//发送请求的URL地址 
        JSONObject jsonObject = new JSONObject();//封装参数
        jsonObject.put("phone", "xxxx");
        jsonObject.put("agent", "xxxx");
        String params=jsonObject.toString();
        sendPostHttpClient(url, params);
}    
        
        
public String sendPostHttpClient(String url, String data) {
        String response = null;
        try {
            CloseableHttpClient httpclient = null;
            CloseableHttpResponse httpresponse = null;
            try {
                httpclient = HttpClients.createDefault();
                HttpPost httppost = new HttpPost(url);
                //StringEntity stringentity = new StringEntity(data, ContentType.create("text/json", "UTF-8"));参数传递文本格式
                StringEntity stringentity = new StringEntity(data, ContentType.create("application/json", "UTF-8"));//参数传递json格式
                httppost.setEntity(stringentity);
                httpresponse = httpclient.execute(httppost);
                response = EntityUtils.toString(httpresponse.getEntity());
                logger.info("response: " + response);
            } finally {
                if (httpclient != null) {
                    httpclient.close();
                }
                if (httpresponse != null) {
                    httpresponse.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
}

 

 

方法二:RestTemplate

public Map<String,String> getShangHaiCarDealCount(String formatTime) {
        Map<String,String> map=new HashMap<>();
        map.put("flag", "fail");
        String cycle = "1";
        String pic_type = "3";
        String qsrq = formatTime;
        String Submit = "显示";
        String zzrq = formatTime;
        
        String result = null;
        try {
            String url = "http://xxxxxxxxxxxxxxxxxxxxxx";
            RestTemplate client = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            //  请勿轻易改变此提交方式,大部分的情况下,提交方式都是表单提交
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            //  封装参数,千万不要替换为Map与HashMap,否则参数无法传递
            MultiValueMap<String, String> params= new LinkedMultiValueMap<String, String>();
            //  也支持中文
            params.add("cycle", cycle);
            params.add("pic_type", pic_type);
            params.add("qsrq", qsrq);
            params.add("Submit", Submit);
            params.add("zzrq", zzrq);
            HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, headers);
            //  执行HTTP请求
            ResponseEntity<String> response = client.exchange(url, HttpMethod.POST, requestEntity, String.class);
            result = response.getBody();
            map.put("result", result);
            map.put("flag", "success");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
    

 

以上是关于java 模拟发送post请求测试的主要内容,如果未能解决你的问题,请参考以下文章

java模拟post请求发送json

Java模拟POST请求发送二进制数据

Java 后台模拟发送 POST GET 请求

java模拟post请求发送json数据

Fiddler模拟POST请求

postman怎么发送post请求