码迷,mamicode.com
首页 > 编程语言 > 详细

java 模拟发送post请求测试

时间:2018-02-26 17:39:53      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:efault   ash   dia   return   提交   params   void   sch   turn   

方法一: 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请求测试

标签:efault   ash   dia   return   提交   params   void   sch   turn   

原文地址:https://www.cnblogs.com/mr-wuxiansheng/p/8473878.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!