作接口測試中常常一些發送的報文是加密後的 或者是進行過壓縮的,因此要很差直接用lr自帶函數去解決。以爲java腳本解決問題仍是很快捷的,至少java有不少方法能夠給你去調用。 java
先說說lr建立java_vuser腳本 ,在創建腳本界面選擇Create/Edit Script-->New Script-->選擇java協議-->java_vuser 創建腳本,在run-time-setting 設置下classpath。導包和java同樣的 再就是import com.lr_class.*這句 是在腳本目錄創建com文件夾,在com中在家裏lr_class文件夾,將用到的類文件放在其中就可用那句話就能夠進行調用了。 web
下面的代碼就不用講了 就是個例子而已(由於這個腳本要使用到gzip壓縮和rxxx加密(公司自編寫加密方式),因此採用java_vuser方式編寫。聽說http協議中使 web_custom_request函數中的 BodyBinary參數能夠發送字節流,試過可是沒有成功,有那位大大回答能夠留言告訴一下,在此謝過先!) apache
/** Script Description:XXXX;Statistics created by hyp;2012-11-02 */
import lrapi.lr;
import com.lr_class.*;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.DefaultHttpClient;
public class Actions
{ public int init() throws Throwable {
return 0;
}
public int action() throws Throwable {
// String url ="http://test1.gionee.com/stats/uploadData";
String url ="http://42.121.88.33/stats/uploadData";
// String url ="http://18.8.6.70:8080/stats/uploadData";
int code;
byte[] dataBytes= Fdata.putData("<aa>","<bb>","<cc>","<dd>","<ee>",1,1,5,1,1348042886,4,13432909036L,1234567890123455L,1,1);
System.out.println(dataBytes);
byte[] b = UnGZIP.compressToByte(dataBytes);
b = XXXXXRC4.code(b);
System.out.println(b);
lr.start_transaction("發送");
ByteArrayInputStream bs = null ;
bs = new ByteArrayInputStream(b, 0, b.length);
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/octet-stream");
HttpEntity he = new InputStreamEntity(bs, b.length);
httpPost.setEntity(he);
HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);
code = httpResponse.getStatusLine().getStatusCode();
if (code == 901) {
lr.output_message("statistics成功 code:"+code);
lr.end_transaction("發送", lr.PASS);
}
else{
lr.error_message("statistics失敗 code:"+code);
lr.end_transaction("發送", lr.FAIL);
}
return 0;
}
public int end() throws Throwable {
return 0;
}
}
api