背景:由http转https,采购解析https的板卡,评估性能。使用LR测试时发现不同网段的请求有的网段会被拦截(具体原因还未来得及细查)。随后改用jmeter测试。顺便就研究了一下能否绕过https证书,做了个demo记录一下,备用
一个能用jmeter调用的java请求。添加依赖jar:httpclient相关包、jmeter相关包(ApacheJMeter_core.jar、ApacheJMeter_http.jar、ApacheJMeter_java.jar、ApacheJMeter_components.jar、jorphan-3.0.jar)
package com.demo.java.test;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
public class HttpsClientTest extends AbstractJavaSamplerClient {
public static String send(String url, Map map, String encoding)
throws KeyManagementException, NoSuchAlgorithmException, ClientProtocolException, IOException {
String body = "";
// 采用绕过验证的方式处理https请求
SSLContext sslcontext = createDefineSSL();
// 设置协议http和https对应的处理socket链接工厂的对象
Registry socketFactoryRegistry = RegistryBuilder.create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", new SSLConnectionSocketFactory(sslcontext)).build();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
HttpClients.custom().setConnectionManager(connManager);
// 创建自定义的httpclient对象
CloseableHttpClient client = HttpClients.custom().setConnectionManager(connManager).build();
// CloseableHttpClient client = HttpClients.createDefault();
// 创建请求对象
HttpGet httpGet = new HttpGet(url);
// 装填参数
List nvps = new ArrayList();
if (map != null) {
for (Entry entry : map.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
// 设置参数到请求对象中
System.out.println("请求地址:" + url);
System.out.println("请求参数:" + nvps.toString());
// 设置header信息
httpGet.setHeader("Content-type", "application/x-www-form-urlencoded");
httpGet.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
// 执行请求操作,并拿到结果(同步阻塞)
CloseableHttpResponse response = client.execute(httpGet);
// 获取结果实体
HttpEntity entity = response.getEntity();
if (entity != null) {
// 按指定编码转换结果实体为String类型
body = EntityUtils.toString(entity, encoding);
}
EntityUtils.consume(entity);
// 释放链接
response.close();
return body;
}
public static SSLContext createDefineSSL() throws NoSuchAlgorithmException, KeyManagementException {
SSLContext sc = SSLContext.getInstance("SSLv3");
// 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
X509TrustManager trustManager = new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
String paramString) throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
String paramString) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sc.init(null, new TrustManager[] { trustManager }, null);
return sc;
}
public Arguments getDefaultParameters() {
Arguments params = new Arguments();
params.addArgument("url", "https://10.255.251.33");
return params;
}
public void setupTest(JavaSamplerContext arg0) {
}
public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
SampleResult sr = new SampleResult();
sr.setSampleLabel("https");
try {
sr.sampleStart();
sr.setResponseData("from provider:" + send(javaSamplerContext.getParameter("url"), null, "utf-8"), null);
// System.out.println("from provider:" +responseInfo.getResultMsg());
sr.setDataType(SampleResult.TEXT);
sr.setSuccessful(true);
} catch (Exception e) {
e.printStackTrace();
} finally {
sr.sampleEnd();
}
return sr;
}
public void teardownTest(JavaSamplerContext arg0) {
}
//顺便测试一下
public static void main(String[] args)
throws ParseException, IOException, KeyManagementException, NoSuchAlgorithmException {
System.out.println(send("https://10.255.251.33", null, "utf-8"));
}
}