最近要测试并发下公司网站的注册情况,需要模拟POST动作
下面记录下代码
public class TestReg { public boolean doReg() throws IOException{ HttpPost httpost = new HttpPost("http://xxxx.xxxx.com/register/succeed/"); HttpClient httpclient = new DefaultHttpClient(); List<NameValuePair> nvps=new ArrayList<NameValuePair>(); //需要通过POST提交的参数 nvps.add(new BasicNameValuePair("realname", "陈一一")); nvps.add(new BasicNameValuePair("pass1", "123456")); nvps.add(new BasicNameValuePair("pass2", "123465")); nvps.add(new BasicNameValuePair("parentemail", "[email protected]")); nvps.add(new BasicNameValuePair("icoType", "1")); httpost.setEntity(new UrlEncodedFormEntity(nvps,"utf-8")); //增加COOKIE httpost.setHeader("Cookie", "TOKEN=1234567890"); HttpResponse response = httpclient.execute(httpost); HttpEntity entity = response.getEntity(); BufferedReader br=new BufferedReader(new InputStreamReader(entity.getContent())); String temp=""; int i=0; boolean bo=false; while((temp=br.readLine())!=null){ //System.out.println(i); //System.out.println(temp); if(temp.indexOf("我的盒号")>0){ System.out.println(temp); bo=true; } } br.close(); return bo; } public static void main(String[] args) { //总注册数 int sumRuntimes=20; //最大线程数 int maxThread=5; threadPoint=maxThread; TestReg tr=new TestReg(); int nowRuntiems=0; int maxRuntiems=sumRuntimes/maxThread; boolean runPoint=true; while(runPoint){ while(threadPoint==maxThread ){ threadPoint=0; tr.doTest(maxThread); nowRuntiems++; System.out.println("开始"+nowRuntiems); } if(nowRuntiems==maxRuntiems){ runPoint=false; } } } static int threadPoint=0; public void doTest(int maxThread){ int point=0; while(point++<maxThread){ new Thread(new Runnable(){ public void run() { try { Date sd=new Date(); if(doReg()){ System.out.println("注册成功!"); }else{ System.out.println("注册失败"); } Date ed=new Date(); System.out.println("运行时间="+(ed.getTime()-sd.getTime())); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ //完成执行线程 threadPoint++; //System.out.println(threadPoint); } } }).start(); } } }