问自己今天是否比明天进步了 !

java,激活压力测试 !

上一篇 / 下一篇  2012-02-16 17:44:08 / 个人分类:Java And Android 学习记录

激活功能测试代码: 
package com.jieless.publiclib;
//import java.rmi.RemoteException;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;                                                                            
import org.apache.http.util.EntityUtils;

import android.net.ParseException;

public class ActivityThreadClient extends Thread  {
int num;
public void setNum(int n) {
 this.num = n;
}
//在run里面变动业务处理的逻辑
public void run() {
int i=0;
String requestxml="<xmedia>"+
"<sysInfo>"+
"<device>test</device>"+
"<os__arch>test</os__arch>"+
"<os__name>test</os__name>"+
"<os__version>test</os__version>"+
"<sdkVersion>test</sdkVersion>"+
"<sdkInt>test</sdkInt>"+
"<codename>test</codename>"+
"<incremental>test</incremental>"+
"<release>test</release>"+
"<model>test</model>"+
"<product>test</product>"+
"<language>中文</language>"+
"<screen>300X200</screen>"+
"</sysInfo>"+
"<activationInfo>"+
"<license>670AB5E2439459FC00DEB7D6E0128ADD</license>"+
"<hwid>test_" + i * 10+ "</hwid>"+
"<sequence></sequence>"+
"</activationInfo>"+
"</xmedia>";
String apptype = "xmedia";
 while (i < 10) {
  System.out.println("==========第"+ num + "线程里面,第"+ i + "次请求START=======");
  DefaultHttpClient httpClient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://service.wujiecn.com/xservice/deviceActive/active");
MultipartEntity reqEntity = new MultipartEntity();  
       try {
reqEntity.addPart("requestxml", new StringBody(requestxml));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
       //设置请求  
       httppost.setEntity(reqEntity);  
       //执行  
       HttpResponse response = null;
try {
response = httpClient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
       if(HttpStatus.SC_OK == response.getStatusLine().getStatusCode()){  
           HttpEntity entity = response.getEntity();  
           //显示内容  
           if (entity != null) {  
               try {
System.out.println(EntityUtils.toString(entity));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}  
           }  else{
           System.out.println("Erro");
           }
           if (entity != null) {  
               try {
entity.consumeContent();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}  
           }  
       }else{
           System.out.println("Erro");
            }
       
       httpClient.getConnectionManager().shutdown();
 }
 
   i++;
   System.out.println(
    "***********第" + num + "线程里面,第" + i + "次请求END*****");
   
 

 }



 
/**
* @param args
*/
public static void main(String[] args) {

}

}


创建log代码:



package com.jieless.publiclib;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;


public class CreateLog {

final String FILENAME = "/PressureTest";
final String SUFFIX = ".log";
String foldername = "D:";
public CreateLog() {

}

public void CreateFile() {
String filename = foldername + FILENAME + SUFFIX;
File folder = new File(filename);
if (folder.exists()) {
folder.delete();
try {
folder.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
} else if (!folder.exists()) {
try {
folder.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}

}

public void WriteLog(String sb) {
String stringToWrite = sb.toString();
String targetPath = foldername + FILENAME + SUFFIX;
File targetFile = new File(targetPath);
if (targetFile != null) {
OutputStreamWriter osw;
try {
osw = new OutputStreamWriter(new FileOutputStream(targetFile,
true), "utf-8");
try {
osw.write(stringToWrite + "\r\n");
osw.flush();
osw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}

具体详情请参照:附件 !





TAG:

 

评分:0

我来说两句

Open Toolbar