/*
* LoadRunner Java script. (Build: _build_number_)
*
* Script Description:
*
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import lrapi.lr;
public class Actions
{
public static String Sendgift(String url, String param,String key) {
String result = "";
int proxyPort=111;
String proxyHost="1.1.1.1";
boolean isproxy=false;
OutputStreamWriter out = null;
BufferedReader in = null;
try {
URL realUrl = new URL(url);
HttpURLConnection conn = null;
if (isproxy) {
//使用代理IP
@SuppressWarnings("static-access")
Proxy proxy = new Proxy(Proxy.Type.DIRECT.HTTP, new InetSocketAddress(proxyHost, proxyPort));
conn = (HttpURLConnection) realUrl.openConnection(proxy); //使用代理IP,打开和URL之间的连接
}else {
//不使用代理IP
conn = (HttpURLConnection) realUrl.openConnection(); //不使用代理IP,打开和URL之间的连接
}
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// POST方法
conn.setRequestMethod("POST");
// 设置通用的请求属性
conn.setConnectTimeout(5 * 1000);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36");
conn.setRequestProperty("Host", "www.sjlive.cn");
conn.setRequestProperty("key", key);
// 建立真实HTTP连接
conn.connect();
// 获取URLConnection对象对应的输出流
out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
// 发送请求参数
out.write(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String line;
while ((line = in.readLine()) != null) {
result = result + line + "
";
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!" + e);
e.printStackTrace();
} finally {
try{
if(out != null){
out.close();
}
if(in != null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}
public int init() throws Throwable {
return 0;
}//end of init
public int action() throws Throwable {
//主函数执行部分
System.out.println("服务器返回值:" + body);
if(body.indexOf(""Code": "1"")==-1)
{
lr.error_message("error"+body);
}
return 0;
}//end of action
public int end() throws Throwable {
return 0;
}//end of end
}