Java模拟发送post请求

项目要求:模拟100个温湿度设备发送温湿度数据进行压测,查看数据是否有叠加且显示正确,因为测试环境简陋,没有100个温湿度设备,只能通过调用接口模拟发送请求,由于每次发送的请求要求正文某些元素值要唯一,所以通过传参方式,每间隔一秒发送一次请求,发送满四条后间隔10分钟再次发送,写的是死循环,代码如下:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class DGStr {
	static String Tag1="";
	static String Tag2="";
	static String Tag3="";
	static String Tag4="";
	static int k=0;
	final static String url = "url地址"; 
    public static void main(String[] args) {
    	for(int j=3;j<80;j++) {
    		if(j>=3&&j<=19) {
    			Tag1=Tag1+"{\"id\":"+j+", \"t\":25.0, \"bat\":3},";
    			continue;
    		}
    		if(j>=21&&j<=39)
    		{
    			Tag2=Tag2+"{\"id\":"+j+", \"t\":25.0, \"bat\":3},";
    			continue;
    		}
    		if(j>=41&&j<=59)
    		{
    			Tag3=Tag3+"{\"id\":"+j+", \"t\":25.0, \"bat\":3},";
    			continue;
    		}
    		if(j>=61&&j<=79)
    		{
    			Tag4=Tag4+"{\"id\":"+j+", \"t\":25.0, \"bat\":3},";
    			continue;
    		}
   		//Tag1=Tag1+"{\"id\":"+j+", \"t\":25.0, \"bat\":3},";
    		
    	}
    	Tag1=Tag1+"{\"id\":20, \"t\":25.0, \"bat\":3}";
    	Tag2=Tag2+"{\"id\":40, \"t\":25.0, \"bat\":3}";
    	Tag3=Tag3+"{\"id\":60, \"t\":25.0, \"bat\":3}";
    	Tag4=Tag4+"{\"id\":80, \"t\":25.0, \"bat\":3}";
    	String[] params= {"{\"lac\":16794,"
                + "\"cid\":19531,"+ "\"grp\":1," + "\"nodes\":"+"["+Tag1+"]"+"}","{\"lac\":16794,"
                        + "\"cid\":19531,"+ "\"grp\":1," + "\"nodes\":"+"["+Tag2+"]"+"}","{\"lac\":16794,"
                                + "\"cid\":19531,"+ "\"grp\":1," + "\"nodes\":"+"["+Tag3+"]"+"}","{\"lac\":16794,"
                                       + "\"cid\":19531,"+ "\"grp\":1," + "\"nodes\":"+"["+Tag4+"]"+"}"};   
    	try {
    		while(true) {   	
        		for(k=0;k<4;k++) {
        			post(url, params[k]);
        			Thread.sleep(1000);
        		}
        		Thread.sleep(60000);
        		
    		}
        		
        		
    		
    	}catch(InterruptedException e) {
    		 e.printStackTrace();
    	}
    }

    public static String post(String strURL, String params) {
        System.out.println(strURL);
        System.out.println(params);
        BufferedReader reader = null;
        try {
            URL url = new URL(strURL);// 创建连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestMethod("POST"); // 设置请求方式
            connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
            connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
            connection.connect();
            //一定要用BufferedReader 来接收响应, 使用字节来接收响应的方法是接收不到内容的
            OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码
            out.append(params);
            //System.out.println(params);
            	
            
            out.flush();
            out.close();
            // 读取响应
            reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String line;
            String res = "";
            while ((line = reader.readLine()) != null) {
                res += line;
            }
            reader.close();
            

            return res;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "error"; // 自定义错误信息
    }


}

运行结果:

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页