背景:
我在用RPT进行一个Web测试,其中一个网页要求用户从本地选中一个Excel文件上传到Server。我用RPT录制下来的HTTP Request是这个样子的:POST /cn/dsr/claim/importRunrateClaimProduct HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*
Referer: https://amshttp1/cn/dsr/claim/loadImportRunrateClaimProduct
Accept-Language: en-us
Content-Type: multipart/form-data; boundary=---------------------------7d81f1980b58
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)
Host: amshttp1
Content-Length: 14761
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: cmRS=&t1=1213760446609&t2=1213760448171&t3=1213760483031&t4=1213760446343&fti=1213760483031&fn=%3A0%3BimportForm%3A1%3B&ac=1:S&fd=&uer=&fu=importRunrateClaimProduct&pi=IBM%20xDSR%u7CFB%u7EDF%20-%20%u5BFC%u5165%u7533%u62A5%20-%20%u4E2D%u56FD%20-%20amshttp1/cn/dsr/claim/loadImportRunrateClaimProduct&ho=data.coremetrics.com/cm%3F&ci=90130510
ServerSuppliedCookie: JSESSIONID=0000aJQ397zqyu686GDTEdif-_T:-1-----------------------------7d81f1980b58
Content-Disposition: form-data; name="file"; filename="D:\!!!--IGA---!!!\XDSR\Sample\claim.xls"
Content-Type: application/vnd.ms-excel邢唷
..... (这里是一些乱码,应该是读取的文件内容吧)
-----------------------------7d81f1980b58
Content-Disposition: form-data; name="import.x"30
-----------------------------7d81f1980b58
Content-Disposition: form-data; name="import.y"20
-----------------------------7d81f1980b58--问题:
现在我需要下如下方法内除Request Header的部分都返回。注,如下方法只能返回String. 而且要求返回的内容跟原始的录制下来的用IE上传文件时Request里的Data一样,也就是说,我可以拿此方法返回的String直接插入到我的HTTP Request中。十分感谢!
方法:
public String exec(ITestExecutionServices tes, String[] args) { }
这里可以不用管这tes和args.要返回的内容:-----------------------------7d81f1980b58
Content-Disposition: form-data; name="file"; filename="D:\!!!--IGA---!!!\XDSR\Sample\claim.xls"
Content-Type: application/vnd.ms-excel邢唷
..... (这里是一些乱码,应该是读取的文件内容吧)
-----------------------------7d81f1980b58
Content-Disposition: form-data; name="import.x"30
-----------------------------7d81f1980b58
Content-Disposition: form-data; name="import.y"20
-----------------------------7d81f1980b58--
我在用RPT进行一个Web测试,其中一个网页要求用户从本地选中一个Excel文件上传到Server。我用RPT录制下来的HTTP Request是这个样子的:POST /cn/dsr/claim/importRunrateClaimProduct HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*
Referer: https://amshttp1/cn/dsr/claim/loadImportRunrateClaimProduct
Accept-Language: en-us
Content-Type: multipart/form-data; boundary=---------------------------7d81f1980b58
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)
Host: amshttp1
Content-Length: 14761
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: cmRS=&t1=1213760446609&t2=1213760448171&t3=1213760483031&t4=1213760446343&fti=1213760483031&fn=%3A0%3BimportForm%3A1%3B&ac=1:S&fd=&uer=&fu=importRunrateClaimProduct&pi=IBM%20xDSR%u7CFB%u7EDF%20-%20%u5BFC%u5165%u7533%u62A5%20-%20%u4E2D%u56FD%20-%20amshttp1/cn/dsr/claim/loadImportRunrateClaimProduct&ho=data.coremetrics.com/cm%3F&ci=90130510
ServerSuppliedCookie: JSESSIONID=0000aJQ397zqyu686GDTEdif-_T:-1-----------------------------7d81f1980b58
Content-Disposition: form-data; name="file"; filename="D:\!!!--IGA---!!!\XDSR\Sample\claim.xls"
Content-Type: application/vnd.ms-excel邢唷
..... (这里是一些乱码,应该是读取的文件内容吧)
-----------------------------7d81f1980b58
Content-Disposition: form-data; name="import.x"30
-----------------------------7d81f1980b58
Content-Disposition: form-data; name="import.y"20
-----------------------------7d81f1980b58--问题:
现在我需要下如下方法内除Request Header的部分都返回。注,如下方法只能返回String. 而且要求返回的内容跟原始的录制下来的用IE上传文件时Request里的Data一样,也就是说,我可以拿此方法返回的String直接插入到我的HTTP Request中。十分感谢!
方法:
public String exec(ITestExecutionServices tes, String[] args) { }
这里可以不用管这tes和args.要返回的内容:-----------------------------7d81f1980b58
Content-Disposition: form-data; name="file"; filename="D:\!!!--IGA---!!!\XDSR\Sample\claim.xls"
Content-Type: application/vnd.ms-excel邢唷
..... (这里是一些乱码,应该是读取的文件内容吧)
-----------------------------7d81f1980b58
Content-Disposition: form-data; name="import.x"30
-----------------------------7d81f1980b58
Content-Disposition: form-data; name="import.y"20
-----------------------------7d81f1980b58--
楼主截止到2008-06-20 00:15:38的汇总数据:
注册日期:2003-8-28
上次登录:2008-6-19
发帖数:2 发帖分:110
结贴数:0 结贴分:0
结贴率:0.00 % 结分率:0.00 %
如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
1.不能肯定这种方法生成的Request跟用IE提交时Request是不是一模一样的。
2.Code中几个数据可能参数化,如Boundary和FileName等。但是不知道最后的30和20是什么意思。import.x和import.y是什么。所以暂时写了固定的值。请高手指教。public String exec(ITestExecutionServices tes, String[] args) {
String strBoundary=null;
String strFileName="D:\\claim.xls"; //TBD.
BufferedInputStream bis=null;
byte[] data=null;
try{
bis=new BufferedInputStream(new FileInputStream(strFileName));
data=new byte[bis.available()];
bis.read(data,0,data.length);
bis.close(); tes.getTestLogManager().reportMessage("ReadFile Succeed:"+strFileName+""+data.length);
}
catch (IOException e) {
tes.getTestLogManager().reportMessage("ReadFile Failed:"+strFileName);
}
//wirte back to a local file, just test the custom code.
BufferedOutputStream bos=null;
try{
bos=new BufferedOutputStream(new FileOutputStream("D:\\claimout.xls"));
bos.write(data,0,data.length);
bos.close();
tes.getTestLogManager().reportMessage("WriteFile Succeed");
}
catch (IOException e) {
tes.getTestLogManager().reportMessage("WriteFile Failed");
}
strBoundary=new String(""+(Math.random()+Math.random())).substring(2);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(baos);
try{
dos.writeBytes("-----------------------------"+strBoundary);
dos.writeBytes("\r\nContent-Disposition: form-data; name=\"file\"; filename=\""+strFileName+"\"");
dos.writeBytes("\r\nContent-Type: application/vnd.ms-excel");
dos.writeBytes("\r\n\r\n");
dos.writeBytes(new String(data));//The content of the uploaded file
dos.writeBytes("\r\n");
dos.writeBytes("-----------------------------"+strBoundary);
dos.writeBytes("\r\nContent-Disposition: form-data; name=\"import.x\"");
dos.writeBytes("\r\n\r\n");
dos.writeBytes("30"); //what is 30?
dos.writeBytes("\r\n");
dos.writeBytes("-----------------------------"+strBoundary);
dos.writeBytes("\r\nContent-Disposition: form-data; name=\"import.y\"");
dos.writeBytes("\r\n\r\n");
dos.writeBytes("20"); //what is 20? dos.writeBytes("\r\n");
dos.writeBytes("-----------------------------"+strBoundary+"--");
dos.writeBytes("\r\n");
dos.close();
baos.close();
tes.getTestLogManager().reportMessage("Create RequestData Succeed");
}
catch (Exception e) {
tes.getTestLogManager().reportMessage("Create RequestData Failed");
}
return baos.toString();
}
String sRetUrlFilePathName = null;
if (null == formFile) {
return sRetUrlFilePathName;
}
if(!checkFileType(formFile.getFileName()) || !checkFileSize(formFile.getFileSize()) ){
if (logger.isWarnEnabled()) {
logger.warn("File '" + formFile.getFileName() +"' over size or type mismatch, upload failed.");
}
return sRetUrlFilePathName;
}
int iIndex = formFile.getFileName().lastIndexOf(".");
String fileName = formFile.getFileName().substring(iIndex);
if (logger.isDebugEnabled()) {
logger.debug("fileName: " + fileName);
}
String fullFilePathName = getUploadRealPathFileName(fileName,userSession);
if (null != fullFilePathName) {
OutputStream bos = null;
InputStream stream = null;
try {
bos = new FileOutputStream(fullFilePathName);
stream = formFile.getInputStream();
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
sRetUrlFilePathName = getUrlPathFileName(fullFilePathName);
} catch (FileNotFoundException ex) {
if (logger.isErrorEnabled()) {
logger.error(ex.getStackTrace(), ex);
}
} catch (IOException ex) {
if (logger.isErrorEnabled()) {
logger.error(ex.getStackTrace(), ex);
}
} catch (Exception ex) {
if (logger.isErrorEnabled()) {
logger.error(ex.getStackTrace(), ex);
}
} finally {
if (null != bos) {
try {
bos.close();
} catch (Exception ex) {
if (logger.isErrorEnabled()) {
logger.error(ex.getStackTrace(), ex);
}
}
}
if (null != stream) {
try {
stream.close();
} catch (Exception ex) {
if (logger.isErrorEnabled()) {
logger.error(ex.getStackTrace(), ex);
}
}
}
}
}
return sRetUrlFilePathName;
}