zoukankan      html  css  js  c++  java
  • 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;
        }
        
  • 相关阅读:
    uip UDPclient模式通信移植,p本地ort可以是无规
    正则表达式摘要
    Regular expression
    正则-合乎规则
    通配符-通配
    正则表达式总结
    判断大盘筑顶的方法
    筑顶和下跌规律
    股票的筑顶危险信号
    股票筑顶的基本特征
  • 原文地址:https://www.cnblogs.com/mr-wuxiansheng/p/8473878.html
Copyright © 2011-2022 走看看