栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Java

jmeter http sample自定义http请求方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

jmeter http sample自定义http请求方法

HTTPSamplerFactory.class
package org.apache.jmeter.protocol.http.sampler;

import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.util.JOrphanUtils;

public final class HTTPSamplerFactory {
  public static final String HTTP_SAMPLER_JAVA = "HTTPSampler";
  
  public static final String HTTP_SAMPLER_APACHE = "HTTPSampler2";
  
  public static final String IMPL_HTTP_CLIENT4 = "HttpClient4";
  
  public static final String IMPL_HTTP_CLIENT3_1 = "HttpClient3.1";
  
  public static final String IMPL_JAVA = "Java";
  
  public static final String IMPL_APPRF = "Apprf";
  
  public static final String DEFAULT_CLASSNAME = JMeterUtils.getPropDefault("jmeter.httpsampler", "HttpClient4");
  
  public static HTTPSamplerBase newInstance() {
    return newInstance(DEFAULT_CLASSNAME);
  }
  
  public static HTTPSamplerBase newInstance(String alias) {
    if (alias == null || alias.length() == 0)
      return new HTTPSamplerProxy(); 
    if (alias.equals("HTTPSampler") || alias.equals("Java"))
      return new HTTPSamplerProxy("Java"); 
    if (alias.equals("HttpClient4") || alias.equals("HTTPSampler2") || alias.equals("HttpClient3.1"))
      return new HTTPSamplerProxy("HttpClient4"); 
    if (alias.equals("Apprf"))
      return new HTTPSamplerProxy("Apprf"); 
    throw new IllegalArgumentException("Unknown sampler type: '" + alias + "'");
  }
  
  public static String[] getImplementations() {
    return new String[] { "HttpClient4", "Java", "Apprf" };
  }
  
  public static HTTPAbstractImpl getImplementation(String impl, HTTPSamplerBase base) {
    if ("file".equals(base.getProtocol()))
      return new HTTPFileImpl(base); 
    if (JOrphanUtils.isBlank(impl))
      impl = DEFAULT_CLASSNAME; 
    if ("Java".equals(impl) || "HTTPSampler".equals(impl))
      return new HTTPJavaImpl(base); 
    if ("HttpClient4".equals(impl) || "HttpClient3.1".equals(impl))
      return new HTTPHC4Impl(base); 
    if ("Apprf".equals(impl))
      return new HTTPappImpl(base); 
    throw new IllegalArgumentException("Unknown implementation type: '" + impl + "'");
  }
}
HTTPappImpl.class
else if (getSendParameterValuesAsPostBody()) {
        if (!hasContentTypeHeader) {
          HTTPFileArg file = (files.length > 0) ? files[0] : null;
          if (file != null && file.getMimeType() != null && file.getMimeType().length() > 0) {
            entityEnclosingRequest.setHeader("Content-Type", file.getMimeType());
          } else if (ADD_CONTENT_TYPE_TO_POST_IF_MISSING) {
            entityEnclosingRequest.setHeader("Content-Type", "application/x-www-form-urlencoded");
          } 
        } 
        StringBuilder postBody = new StringBuilder();
        String argstr = null;
        for (PropertyIterator propertyIterator = getArguments().iterator(); propertyIterator.hasNext(); ) {
          JMeterProperty jMeterProperty = propertyIterator.next();
          HTTPArgument arg = (HTTPArgument)jMeterProperty.getObjectValue();
          if (haveContentEncoding) {
            argstr = arg.getEncodedValue(contentEncoding);
          } else {
            argstr = arg.getEncodedValue();
          } 
          String encstr = Base64.getEncoder().encodeToString(argstr.getBytes(StandardCharsets.UTF_8));
          encstr = URLEncoder.encode(encstr);
          postBody.append("data=" + encstr);
        } 
        StringEntity requestEntity = new StringEntity(postBody.toString(), contentEncoding);
        entityEnclosingRequest.setEntity((HttpEntity)requestEntity);
        postedBody.append("data=" + argstr);
  private CloseableHttpResponse executeRequest(CloseableHttpClient httpClient, HttpRequestBase httpRequest, HttpContext localContext, URL url) throws IOException {
    AuthManager authManager = getAuthManager();
    if (authManager != null) {
      Subject subject = authManager.getSubjectForUrl(url);
      if (subject != null)
        try {
          return Subject.doAs(subject, () -> httpClient.execute((HttpUriRequest)httpRequest, localContext));
        } catch (PrivilegedActionException e) {
          log.error("Can't execute httpRequest with subject: {}", subject, e);
          throw new IllegalArgumentException("Can't execute httpRequest with subject:" + subject, e);
        }  
    } 
    CloseableHttpResponse httpresp = httpClient.execute((HttpUriRequest)httpRequest, localContext);
    HttpEntity entity = httpresp.getEntity();
    String oresp = EntityUtils.toString(entity);
    String respstr = oresp.split(""")[3];
    respstr = respstr.replaceAll("\\u003d", "=");
    String decstr = new String(Base64.getDecoder().decode(respstr), "utf-8");
    StringEntity sEntity = new StringEntity(decstr, "utf-8");
    httpresp.setEntity((HttpEntity)sEntity);
    return httpresp;
  }

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1040045.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号