TA的每日心情 | 奋斗 2018-10-31 11:22 |
---|
签到天数: 62 天 连续签到: 1 天 [LV.6]测试旅长
|
3#
楼主 |
发表于 2017-2-17 17:24:10
|
只看该作者
问题我自己解决
没有找到直接使用loadrunner已有函数的方法,幸好的是java vuser中可以直接用java语句来实现想要的功能,所以我直接写了一个方法,如下,希望能给有同样问题的朋友们一点参考
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
private byte[] GetByteFromPost(String strurl,String strparam)
{
HttpPost httpPost = new HttpPost(strurl);
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(15000)
.setConnectTimeout(15000)
.setConnectionRequestTimeout(15000)
.build();
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
StringEntity strEntity=null;
HttpEntity entity = null;
byte[] responseContent = null;
try
{
// 创建默认的httpClient实例.
httpClient = HttpClients.createDefault();
strEntity = new StringEntity(strparam);
strEntity.setContentEncoding("UTF-8");
strEntity.setContentType("text/plain; charset=UTF-8");
httpPost.setEntity(strEntity);
httpPost.setConfig(requestConfig);
// 执行请求
response = httpClient.execute(httpPost);
entity = response.getEntity();
responseContent = EntityUtils.toByteArray(entity);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 关闭连接,释放资源
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return responseContent;
}
另外说明下,要保证运行时设置里的Classpath没有多余的jar,否则可能会报错 |
|