|
/*
* LoadRunner Java script. (Build: 3020)
*
* Script Description:
*
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.util.UUID;
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.PostMethod;
import com.sun.org.apache.xpath.internal.operations.String;
import lrapi.lr;
public class Actions
{
public int init() throws Throwable {
return 0;
}//end of init
public int action() throws Throwable {
// TODO Auto-generated method stub
String username = "test01";
String url = "http://129.168.200.80/omm_spc";
String url2 = "http://129.168.200.80/omm_spc/security/InterceptLogin.do";
String url3 = "http://129.168.200.80/omm_spc/j_spring_security_check?j_username=ljw"+ username +
"&j_password=12345678&_spring_security_remember_me=null";
HttpClient client = new HttpClient();
String session_id;
session_id = WebLoginPage(client, url, url2, url3);
System.out.println(session_id);
String fileUuid = "11111111eeeeeee111111eeeee";
String downloadURL = "http://129.168.200.80/omm_spc/caseFile/download.do?fileUuid="+ fileUuid ;
GetMethod getDownLoad = new GetMethod(downloadURL);
getDownLoad.addRequestHeader("cookie", session_id);
//下载文件的保存路径
String filePath_down = "F:\\";
// 通过文件路径创建File对象,并作为参数传到文件下载模块中
File file = new File(filePath_down + UUID.randomUUID().toString());
int aaa = client.executeMethod(getDownLoad);
InputStream is = getDownLoad.getResponseBodyAsStream();
FileOutputStream fos = new FileOutputStream(file);
try {
byte[] buffer = new byte[8096];
int read;
while (-1 != (read = is.read(buffer))) {
//fos.write(buffer, 0, read);
}
} finally {
is.close();
fos.close();
}
return 0;
}//end of action
public int end() throws Throwable {
return 0;
}//end of end
public String WebLoginPage(HttpClient client, String webLogin, String interceptLogin,
String redirectLogin) throws HttpException, IOException {
String session_id = null;
// 构造HttpClient的实例
// 创建GET方法的实例,模拟打开首页
GetMethod getLoginPage = new GetMethod(webLogin);
Header[] getLoginPageHeaders = null;
try {
int responseCode = client.executeMethod(getLoginPage);
getLoginPageHeaders = getLoginPage.getResponseHeaders();
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 在返回的cookie中获取session_id相关内容
String cook_session_id = null;
for (Header getLoginPageHeader : getLoginPageHeaders) {
if (getLoginPageHeader.getName().equals("Set-Cookie")) {
cook_session_id = getLoginPageHeader.getValue();
}
}
// 截取session_id
session_id = cook_session_id.split(";")[0];
if (null == session_id) {
System.out.println("can't find session_id");
}
// 创建POST方法的实例,登陆
PostMethod interceptLoginPage = new PostMethod(interceptLogin);
interceptLoginPage.addRequestHeader("Cookie", session_id);
interceptLoginPage.addParameter("j_username", "admin");
interceptLoginPage.addParameter("j_username", "Admin@123");
client.executeMethod(interceptLoginPage);
// 创建POST方法的实例,重定向请求
GetMethod redirectLoginPage = new GetMethod(redirectLogin);
redirectLoginPage.addRequestHeader("Cookie", session_id);
int respanceCode = client.executeMethod(redirectLoginPage);
if (200 == respanceCode) {
System.out.println("login success");
}
return session_id;
}
} |
|