码迷,mamicode.com
首页 > 编程语言 > 详细

jmeter java请求

时间:2015-07-29 15:33:28      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

demo下载地址http://yun.baidu.com/share/link?shareid=4277735898&uk=925574576

1、引用jmeter的jar包

到jmeter的保存目录下lib这个文件夹除了自己加的jar包,全部添加到项目中,并build path

技术分享

2、创建http请求类

http请求引用了这个jar包,javax.servlet-api-3.1.0.jar,项目写完的时候,要把这个jar包一起打包,这个部分可以自己实现

技术分享
package com.milan.util;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
//import org.codehaus.jackson.map.ObjectMapper;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.io.*;
import java.util.*;

/**
 * 
 * HTTP 帮助类
 *
 */
public class HttpHelper
{
    /** URL 地址分隔符 */
    public static final String URL_PATH_SEPARATOR    = "/";
    /** HTTP URL 标识 */
    public static final String HTTP_SCHEMA            = "http";
    /** HTTPS URL 标识 */
    public static final String HTTPS_SCHEMA            = "https";
    /** HTTP 默认端口 */
    public static final int HTTP_DEFAULT_PORT        = 80;
    /** HTTPS 默认端口 */
    public static final int HTTPS_DEFAULT_PORT        = 443;
    /** 默认缓冲区大小 */
    private static final int DEFAULT_BUFFER_SIZE    = 4096;
    
    private static final String METHOD_GET = "GET";
    private static final String METHOD_POST = "POST";
    private static ServletContext servletContext;
    
    /** 获取 {@link ServletContext} */
    public static ServletContext getServletContext()
    {
        return servletContext;
    }

    private static final String defaultContentEncoding = "UTF-8";

    /** 向页面输出文本内容 */
    public final static void writeString(HttpURLConnection conn, String content, String charsetName) throws IOException
    {
        writeString(conn.getOutputStream(), content, charsetName);
    }

    /** 向页面输出文本内容 */
    public final static void writeString(HttpServletResponse res, String content, String charsetName) throws IOException
    {
        writeString(res.getOutputStream(), content, charsetName);
    }

    /** 向页面输出文本内容 */
    public final static void writeString(OutputStream os, String content, String charsetName) throws IOException
    {
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(os, charsetName));
        
        pw.write(content);
        pw.flush();
        pw.close();
    }

    /** 向页面输出字节内容 */
    public final static void writeBytes(HttpURLConnection conn, byte[] content) throws IOException
    {
        writeBytes(conn.getOutputStream(), content);
    }

    /** 向页面输出字节内容 */
    public final static void writeBytes(HttpServletResponse res, byte[] content) throws IOException
    {
        writeBytes(res.getOutputStream(), content);
    }

    /** 向页面输出字节内容 */
    public final static void writeBytes(OutputStream os, byte[] content) throws IOException
    {
        BufferedOutputStream bos = new BufferedOutputStream(os);
        
        bos.write(content);
        bos.flush();
        bos.close();
    }

    /** 读取页面请求的文本内容 */
    public final static String readString(HttpURLConnection conn, boolean escapeReturnChar, String charsetName) throws IOException
    {
        return readString(conn.getInputStream(), escapeReturnChar, charsetName);
    }

    /** 读取页面请求的字节内容 */
    public final static String readString(HttpServletRequest request, boolean escapeReturnChar, String charsetName) throws IOException
    {
        return readString(request.getInputStream(), escapeReturnChar, charsetName);
    }

    /** 读取页面请求的文本内容 */
    public final static String readString(InputStream is, boolean escapeReturnChar, String charsetName) throws IOException
    {
        StringBuilder sb = new StringBuilder();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is, charsetName));
        
        try
        {
            if(escapeReturnChar)
            {
                for(String line = null; (line = rd.readLine()) != null;)
                    sb.append(line);
             }
            else
            {
                int count        = 0;
                char[] array    = new char[DEFAULT_BUFFER_SIZE];
                
                while((count = rd.read(array)) != -1)
                    sb.append(array, 0, count);
            }
        }
        finally
        {
            rd.close();
        }
        
        return sb.toString();
    }
    
    /** 读取页面请求的字节内容 */
    public final static byte[] readBytes(HttpURLConnection conn) throws IOException
    {
        return readBytes(conn.getInputStream(), conn.getContentLength());
    }

    /** 读取页面请求的字节内容 */
    public final static byte[] readBytes(HttpServletRequest request) throws IOException
    {
        return readBytes(request.getInputStream(), request.getContentLength());
    }
    
    /** 读取页面请求的字节内容 */
    public final static byte[] readBytes(InputStream is) throws IOException
    {
        return readBytes(is, 0);
    }

    /** 读取页面请求的字节内容 */
    public final static byte[] readBytes(InputStream is, int length) throws IOException
    {
        byte[] array = null;
        
        if(length > 0)
        {
            array = new byte[length];
        
            int read    = 0;
            int total    = 0;
            
            while((read = is.read(array, total, array.length - total)) != -1)
                total += read;
        }
        else
        {
            List<byte[]> list    = new LinkedList<byte[]>();
            byte[] buffer        = new byte[DEFAULT_BUFFER_SIZE];

            int read    = 0;
            int total    = 0;
            
            for(; (read = is.read(buffer)) != -1; total += read)
            {
                byte[] e = new byte[read];
                System.arraycopy(buffer, 0, e, 0, read);
                list.add(e);
            }
            
            array = new byte[total];
            
            int write = 0;
            for(byte[] e : list)
            {
                System.arraycopy(e, 0, array, write, e.length);
                write += e.length;
            }
        }
        
        return array;
    }
    

    /** 置换常见的 XML 特殊字符 */
    public final static String regulateXMLStr(String src)
    {
        String result = src;
        result = result.replaceAll("&", "&amp;");
        result = result.replaceAll("\"", "&quot;");
        result = result.replaceAll("‘", "&apos;");
        result = result.replaceAll("<", "&lt;");
        result = result.replaceAll(">", "&gt;");
        
        return result;
    }

    /** 置换常见的 HTML 特殊字符 */
    public final static String regulateHtmlStr(String src)
    {
        String result = src;
        result = result.replaceAll("&", "&amp;");
        result = result.replaceAll("\"", "&quot;");
        result = result.replaceAll("<", "&lt;");
        result = result.replaceAll(">", "&gt;");
        result = result.replaceAll("\r\n", "<br/>");
        result = result.replaceAll(" ", "&nbsp;");
        
        return result;
    }
    
    /** 确保 URL 路径的前后存在 URL 路径分隔符 */
    public static final String ensurePath(String path, String defPath)
    {
        if(StringUtils.isEmpty(path))
            path = defPath;
        if(!path.startsWith(URL_PATH_SEPARATOR))
            path = URL_PATH_SEPARATOR + path;
        if(!path.endsWith(URL_PATH_SEPARATOR))
            path = path + URL_PATH_SEPARATOR;
        
        return path;
    }
    
    /** 获取 {@link HttpServletRequest} 的指定属性值 */
    @SuppressWarnings("unchecked")
    public final static <T> T getRequestAttribute(HttpServletRequest request, String name)
    {
        return (T)request.getAttribute(name);
    }

    /** 设置 {@link HttpServletRequest} 的指定属性值 */
    public final static <T> void setRequestAttribute(HttpServletRequest request, String name, T value)
    {
        request.setAttribute(name, value);
    }
    
    /** 删除 {@link HttpServletRequest} 的指定属性值 */
    public final static void removeRequestAttribute(HttpServletRequest request, String name)
    {
        request.removeAttribute(name);
    }

    /** 获取 {@link HttpSession} 的指定属性值 */
    @SuppressWarnings("unchecked")
    public final static <T> T getSessionAttribute(HttpSession session, String name)
    {
        return (T)session.getAttribute(name);
    }

    /** 设置 {@link HttpSession} 的指定属性值 */
    public final static <T> void setSessionAttribute(HttpSession session, String name, T value)
    {
        session.setAttribute(name, value);
    }
    
    /** 删除 {@link HttpSession} 的指定属性值 */
    public final static void removeSessionAttribute(HttpSession session, String name)
    {
        session.removeAttribute(name);
    }
    
    /** 销毁 {@link HttpSession} */
    public final static void invalidateSession(HttpSession session)
    {
        session.invalidate();
    }

    /** 获取 {@link ServletContext} 的指定属性值 */
    @SuppressWarnings("unchecked")
    public final static <T> T getApplicationAttribute(String name)
    {
        return (T)getApplicationAttribute(servletContext, name);
    }

    /** 获取 {@link ServletContext} 的指定属性值 */
    @SuppressWarnings("unchecked")
    public final static <T> T getApplicationAttribute(ServletContext servletContext, String name)
    {
        return (T)servletContext.getAttribute(name);
    }

    /** 设置 {@link ServletContext} 的指定属性值 */
    public final static <T> void setApplicationAttribute(String name, T value)
    {
        setApplicationAttribute(servletContext, name, value);
    }

    /** 设置 {@link ServletContext} 的指定属性值 */
    public final static <T> void setApplicationAttribute(ServletContext servletContext, String name, T value)
    {
        servletContext.setAttribute(name, value);
    }
    
    /** 删除 {@link ServletContext} 的指定属性值 */
    public final static void removeApplicationAttribute(String name)
    {
        removeApplicationAttribute(servletContext, name);
    }
    
    /** 删除 {@link ServletContext} 的指定属性值 */
    public final static void removeApplicationAttribute(ServletContext servletContext, String name)
    {
        servletContext.removeAttribute(name);
    }

    /** 获取 {@link HttpServletRequest} 的指定请求参数值,并去除前后空格 */
    public final static String getParam(HttpServletRequest request, String name)
    {
        String param = getParamNoTrim(request, name);
        if(param != null) return param = param.trim();
        
        return param;
    }

    /** 获取 {@link HttpServletRequest} 的指定请求参数值 */
    public final static String getParamNoTrim(HttpServletRequest request, String name)
    {
        return request.getParameter(name);
    }

    /** 获取 {@link HttpServletRequest} 的参数名称集合 */
    public final static List<String> getParamNames(HttpServletRequest request)
    {
        List<String> names        = new ArrayList<String>();
        Enumeration<String> en    = request.getParameterNames();
        
        while(en.hasMoreElements())
            names.add(en.nextElement());
        
        return names;
    }

    /** 获取 {@link HttpServletRequest} 的指定请求参数值集合 */
    public final static List<String> getParamValues(HttpServletRequest request, String name)
    {
        String[] values = request.getParameterValues(name);
        return values != null ? Arrays.asList(values) : null;
    }
    
    /** 获取 {@link HttpServletRequest} 的所有参数名称和值 */
    public final static Map<String, String[]> getParamMap(HttpServletRequest request)
    {
        return request.getParameterMap();
    }

    

    /** 获取 {@link HttpSession} 对象,如果没有则进行创建。 */
    public final static HttpSession getSession(HttpServletRequest request)
    {
        return getSession(request, true);
    }

    /** 获取 {@link HttpSession} 对象,如果没有则根据参数决定是否创建。 */
    public final static HttpSession getSession(HttpServletRequest request, boolean create)
    {
        return request.getSession(create);
    }

    /** 创建 {@link HttpSession} 对象,如果已存在则返回原对象。 */
    public final static HttpSession createSession(HttpServletRequest request)
    {
        return getSession(request);
    }

    /** 获取所有 {@link Cookie} */
    public final static Cookie[] getCookies(HttpServletRequest request)
    {
        return request.getCookies();
    }
    
    /** 获取指定名称的 {@link Cookie} */
    public final static Cookie getCookie(HttpServletRequest request, String name)
    {
        Cookie cookie = null;
        Cookie[] cookies = request.getCookies();
        
        if(cookies != null)
        {
            for(Cookie c : cookies)
            {
                if(c.getName().equals(name))
                {
                    cookie = c;
                    break;
                }
            }
        }
        
        return cookie;
    }

    /** 获取指定名称的 {@link Cookie} 值,失败返回 null */
    public final static String getCookieValue(HttpServletRequest request, String name)
    {
        String value = null;
        Cookie cookie = getCookie(request, name);
        
        if(cookie != null)
            value = cookie.getValue();
            if (value != null) {
                try {
                    value = URLDecoder.decode(value, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        return value;
    }

    /** 添加 {@link Cookie} */
    public final static void addCookie(HttpServletResponse response, Cookie cookie)
    {
        response.addCookie(cookie);
    }

    /** 添加 {@link Cookie} */
    public final static void addCookie(HttpServletResponse response, String name, String value)
    {
        addCookie(response, new Cookie(name, value));
    }
    
    /** 获取 URL 的  BASE 路径 */
    public final static String getRequestBasePath(HttpServletRequest request)
    {
        String scheme        = request.getScheme();
        int serverPort        = request.getServerPort();
        StringBuilder sb    = new StringBuilder(scheme).append("://").append(request.getServerName());
        
        if    (!(
                (scheme.equals(HTTP_SCHEMA) && serverPort == HTTP_DEFAULT_PORT) ||
                (scheme.equals(HTTPS_SCHEMA) && serverPort == HTTPS_DEFAULT_PORT) 
            ))
                sb.append(":").append(request.getServerPort());
        
            sb.append(request.getContextPath()).append("/");

        return sb.toString();
    }
    
    /** 获取 URL 地址在文件系统的绝对路径,
     * 
     * Servlet 2.4 以上通过 request.getServletContext().getRealPath() 获取,
     * Servlet 2.4 以下通过 request.getRealPath() 获取。
     *  
     */
    @SuppressWarnings("deprecation")
    public final static String getRequestRealPath(HttpServletRequest request, String path)
    {
        if(servletContext != null)
            return servletContext.getRealPath(path);
        else
        {
            try
            {
                Method m = request.getClass().getMethod("getServletContext");
                ServletContext sc = (ServletContext)m.invoke(request);
                return sc.getRealPath(path);
            }
            catch(Exception e)
            {
                return request.getRealPath(path);
            }
        }
    }
    
    /** 获取发送请求的客户端浏览器所在的操作系统平台 */
    public final static String getRequestUserAgentPlatform(HttpServletRequest request)
    {
        int index        = 1;
        String platform    = null;
        String agent    = request.getHeader("user-agent");
        
        if(StringUtils.isNotEmpty(agent))
        {
            int i                = 0;
            StringTokenizer st    = new StringTokenizer(agent, ";");
            
            while(st.hasMoreTokens())
            {
                String token = st.nextToken();
                
                if(i == 0)
                {
                    if(token.toLowerCase().indexOf("compatible") != -1)
                        index = 2;
                }
                else if(i == index)
                {
                    int sep = token.indexOf(")");
                    
                    if(sep != -1)
                        token = token.substring(0, sep);
                    
                    platform = StringUtils.trimToEmpty(token);
                    
                    break;
                }
                
                ++i;
            }
        }

        return platform;
    }
    
    /** 设置 HTTP 的 ‘Content-Type‘ 响应头 */
    public final static void setContentType(HttpServletResponse response, String contentType, String encoding)
    {
        StringBuilder sb = new StringBuilder(contentType);
                
        if(encoding != null)
            sb.append(";charset=").append(encoding);
        
        response.setContentType(sb.toString());
    }
    
    /** 禁止浏览器缓存当前页面 */
    public final static void setNoCacheHeader(HttpServletResponse response)
    {
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
    }
    
    /** 检查请求是否来自非 Windows 系统的浏览器 */
    public final static boolean isRequestNotComeFromWidnows(HttpServletRequest request)
    {
        String agent = request.getHeader("user-agent");
        
        if(StringUtils.isNotEmpty(agent))
            return agent.toLowerCase().indexOf("windows") == -1;
        
        return false;
    }
    /**
     * 发送GET请求
     * @param url
     * @return
     * @throws IOException
     */
    public final static HttpRespons sendGet(String url) throws IOException{
        return send(url,METHOD_GET,new HashMap(),null);
    }
    public final static HttpRespons sendGet(String url, Map<String,String> params) throws IOException{
        return send(url,METHOD_GET,params,null);
    }
    public final static HttpRespons sendGet(String url, Map<String,String> params,Map<String,String> properties) throws IOException{
        return send(url,METHOD_GET,params,properties);
    }
    public final static HttpRespons sendPost(String url) throws IOException{
        return send(url,METHOD_POST,new HashMap(),null);
    }
    public final static HttpRespons sendPost(String url,Map<String,String> params) throws IOException{
        return send(url,METHOD_POST,params,null);
    }
    public final static HttpRespons sendPost(String url, Map<String,String> params,Map<String,String> properties) throws IOException{
        return send(url,METHOD_POST,params,properties);
    }
    /**
     * 发送POST请求
     * @param urlStr 请求地址
     * @param send   发送数据包
     * @return
     * @throws IOException
     */
    public final static HttpRespons sendPost(String urlStr,String send,Map<String,String> headers) throws IOException{
        return send(urlStr,METHOD_POST,send,headers);
    }
    public final static HttpRespons send(String urlStr,String method,String send,Map<String, String> headers) throws IOException{
        HttpURLConnection conn = null;
        URL url = new URL(urlStr);
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod(method);
        conn.setDoOutput(true);  
        conn.setDoInput(true);  
        conn.setUseCaches(false);
        if(headers != null){
            for (String key : headers.keySet()) {
                conn.addRequestProperty(key, headers.get(key));
            }
        }
        conn.getOutputStream().write(send.getBytes());
        conn.getOutputStream().flush();
        conn.getOutputStream().close();
        return makeRespons(urlStr,conn);
    }
    private final static HttpRespons send(String urlStr,String method,Map<String, String> params, Map<String, String> propertys)  
            throws IOException{
        HttpURLConnection conn = null;
        if(METHOD_GET.equals(method) && params !=null && !params.isEmpty()){
            StringBuffer param = new StringBuffer();  
            int i = 0;  
            for (String key : params.keySet()) {  
                if (i == 0)  
                    param.append("?");  
                else  
                    param.append("&");  
                param.append(key).append("=").append(params.get(key));
                i++;  
            }  
            urlStr += param; 
        }
        URL url = new URL(urlStr);
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod(method);
        conn.setDoOutput(true);  
        conn.setDoInput(true);  
        conn.setUseCaches(false);
        if(propertys != null && !propertys.isEmpty()){
            for (String key : propertys.keySet()) {
                conn.addRequestProperty(key, propertys.get(key));
            }
        }
        if(METHOD_POST.equals(method) && params != null){
            StringBuffer param = new StringBuffer();
            for (String key : params.keySet()) {  
                param.append("&");  
                param.append(key).append("=").append(params.get(key));
            }  
            conn.getOutputStream().write(param.toString().getBytes());
            conn.getOutputStream().flush();
            conn.getOutputStream().close();
        }
        return makeRespons(urlStr,conn);
    }
    private final static HttpRespons makeRespons(String urlString,HttpURLConnection urlConnection)throws IOException{
        HttpRespons httpResponser = new HttpRespons();  
        try {  
            InputStream in = urlConnection.getInputStream();  
            BufferedReader bufferedReader = new BufferedReader(  
                    new InputStreamReader(in,"utf-8"));  
            httpResponser.contentCollection = new Vector<String>();  
            StringBuffer temp = new StringBuffer();  
            String line = bufferedReader.readLine();
            while (line != null) {  
                httpResponser.contentCollection.add(line);  
                temp.append(line).append("\r\n");  
                line = bufferedReader.readLine();  
            }  
            bufferedReader.close();  
   
            String ecod = urlConnection.getContentEncoding();  
            
            if (ecod == null){  
                ecod = defaultContentEncoding;  
            }
           
            httpResponser.urlString = urlString;  
   
            httpResponser.defaultPort = urlConnection.getURL().getDefaultPort();  
            httpResponser.file = urlConnection.getURL().getFile();  
            httpResponser.host = urlConnection.getURL().getHost();  
            httpResponser.path = urlConnection.getURL().getPath();  
            httpResponser.port = urlConnection.getURL().getPort();  
            httpResponser.protocol = urlConnection.getURL().getProtocol();  
            httpResponser.query = urlConnection.getURL().getQuery();  
            httpResponser.ref = urlConnection.getURL().getRef();  
            httpResponser.userInfo = urlConnection.getURL().getUserInfo();  
   
            httpResponser.content = new String(temp.toString().getBytes(), "utf-8");  
            httpResponser.contentEncoding = ecod;  
            httpResponser.code =  urlConnection.getResponseCode();  
            httpResponser.message = urlConnection.getResponseMessage();  
            httpResponser.contentType = urlConnection.getContentType();  
            httpResponser.method = urlConnection.getRequestMethod();  
            httpResponser.connectTimeout = urlConnection.getConnectTimeout();  
            httpResponser.readTimeout = urlConnection.getReadTimeout();
            
            return httpResponser;  
        } catch (IOException e) {  
            throw e;  
        } finally {  
            if (urlConnection != null)  
                urlConnection.disconnect();  
        }  
    }
    /**
     * 将请求中的json内容转为Map
     * @param request
     * @return
     * @throws IOException
     */
//    public static Map parseRequestJson(HttpServletRequest request) throws IOException{
//        
//        String str = readString(request, true, request.getCharacterEncoding());
//        ObjectMapper mapper = new ObjectMapper();
//        return mapper.readValue(str,HashMap.class);
//    }
}
View Code
技术分享
package com.milan.util;


import java.util.Vector;

/**
 * 响应对象
 *
 */
public class HttpRespons {
    String urlString;
       
    int defaultPort;
   
    String file;  
   
    String host;  
   
    String path;  
   
    int port;  
   
    String protocol;  
   
    String query;  
   
    String ref;  
   
    String userInfo;  
   
    String contentEncoding;  
   
    String content;  
   
    String contentType;  
   
    int code;  
   
    String message;  
   
    String method;  
   
    int connectTimeout;  
   
    int readTimeout;  
   
    Vector<String> contentCollection;

    public String getUrlString() {
        return urlString;
    }

    public void setUrlString(String urlString) {
        this.urlString = urlString;
    }

    public int getDefaultPort() {
        return defaultPort;
    }

    public void setDefaultPort(int defaultPort) {
        this.defaultPort = defaultPort;
    }

    public String getFile() {
        return file;
    }

    public void setFile(String file) {
        this.file = file;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public String getRef() {
        return ref;
    }

    public void setRef(String ref) {
        this.ref = ref;
    }

    public String getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(String userInfo) {
        this.userInfo = userInfo;
    }

    public String getContentEncoding() {
        return contentEncoding;
    }

    public void setContentEncoding(String contentEncoding) {
        this.contentEncoding = contentEncoding;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public int getConnectTimeout() {
        return connectTimeout;
    }

    public void setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public int getReadTimeout() {
        return readTimeout;
    }

    public void setReadTimeout(int readTimeout) {
        this.readTimeout = readTimeout;
    }

    public Vector<String> getContentCollection() {
        return contentCollection;
    }

    public void setContentCollection(Vector<String> contentCollection) {
        this.contentCollection = contentCollection;
    } 
    
}
View Code

3、创建 java请求类

java请求类需要继承AbstractJavaSamplerClient类和实现Serializable接口。

package com.milan.test;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

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;

import com.milan.util.HttpHelper;

public class JavaRequestTest extends AbstractJavaSamplerClient implements
        Serializable {

    // 设置默认值
    public Arguments getDefaultParameters() {
        Arguments params = new Arguments();
        params.addArgument("url", "http://www.baidu.com");
        params.addArgument("kw", "你好");
        return params;
    }

    public SampleResult runTest(JavaSamplerContext arg0) {
        SampleResult sr = new SampleResult();
        sr.setSampleLabel("java请求测试-MiLan");
        sr.sampleStart();//
        // http请求
        String strReturn = "";
        try {
            strReturn = Send(arg0);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 设置返回值
        sr.setResponseData(strReturn, null);
        sr.setDataType(SampleResult.TEXT);

        System.out.println(strReturn);
        sr.setSuccessful(true);
        sr.sampleEnd();
        return sr;
    }
    // http请求
    private String Send(JavaSamplerContext arg0) throws IOException {
        String url = arg0.getParameter("url", "http://www.baidu.com");
        String kw = arg0.getParameter("kw", "MiLan");
        Map<String, String> map = new HashMap<String, String>();
        map.put("kw", kw);
        return HttpHelper.sendGet(url, map).getContent();

    }
}

4、生成jar包

生成jar包的时候,不能选择jmeter的jar包。只能选择自己有用到的jar包。刚才用到了javax.servlet-api-3.1.0.jar,所以要把这个jar包一起打包。

需要用fatjar打包,不然可能找不到引用jar包的类。不知道怎么用fatjar打包的,可以参考http://www.cnblogs.com/milanmi/p/4651904.html

技术分享

生成jar包后,把jar包拷贝到jmeter的lib/ext目录

5、运行jmeter

技术分享

技术分享

 

jmeter java请求

标签:

原文地址:http://www.cnblogs.com/milanmi/p/4652546.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!