loadrunner,java
/** LoadRunner Java script. (Build: 3020)
*
* Script Description:
*
*/
import java.io.IOException;
import java.util.Map;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import java.io.File;
import lrapi.lr; public class Actions
{
public int init() throws Throwable {
return 0;
}//end of init
public int action() throws Throwable {
HttpClient client = new HttpClient();
String sessionIDByPost=""; //sessionID
String username = "test";
GetMethod openURLPost = new GetMethod("http://*******/omm"); //构造请求:打开登陆页面
//利用用户名和密码构造登陆post请求URL(str/用户名, str/密码)
String loginPostURL = "&&&&&&&&&&&" + username +
"%%%%%%%%%%%%%%%%%%%%%%";
GetMethod loginPost = new GetMethod(loginPostURL); //构造请求:登陆
Header[] getLoginPageHeaders = null; //headers
try {
client.executeMethod(openURLPost); //提交Post请求并返回状态码
getLoginPageHeaders = openURLPost.getResponseHeaders();
String cook_session_id = null; // 在返回的cookie中获取session_id相关内容
for (Header getLoginPageHeader : getLoginPageHeaders) {
if (getLoginPageHeader.getName().equals("Set-Cookie")) {
cook_session_id = getLoginPageHeader.getValue();
}
}
sessionIDByPost = cook_session_id.split(";"); // 截取session
loginPost.addRequestHeader("Cookie", sessionIDByPost); //将sessionID设置到登陆请求里
int respanceCode = client.executeMethod(loginPost); //提交Post请求并返回状态码
if (200 == respanceCode) { //判断返回的状态码是否正确
//
} else {
//
} } catch (HttpException e) {
//
} catch (IOException e) {
//
} catch (Exception e) {
//
} finally {
openURLPost.releaseConnection(); //释放
loginPost.releaseConnection();
}
String filePath = "E:\\GE0223.mpeg"; //待$$$$$$的文件路径
String caseid = "23";
for (int i = 0; i < 1; i++) {
try {
Part[] parts = {new FilePart("filedata", new File(filePath)),
new StringPart("Filename", "GE0223.mpeg"),
new StringPart("name", "10001a.mpeg"),
new StringPart("modifyTime", "1301021316000"),
new StringPart("folderId",caseid),
new StringPart("vehicleTypeCode","小型汽车号牌")
}; //父单位ID
PostMethod filePost = new PostMethod("####################/file.go"); //构造请求
filePost.addRequestHeader("Cookie", sessionIDByPost); //为请求设置cookie
int respanceCode = 0;
try {
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
client.executeMethod(filePost);
} catch (HttpException e) {
//
} catch (IOException e) {
//
} catch (Exception e) {
//
} finally {
filePost.releaseConnection();
}
} catch (Exception e) {
//
}
} //for
return 0;
}//end of action
public int end() throws Throwable {
return 0;
}//end of end
}
页:
[1]