• jmeter 开发自己的java请求 二次开发


    package test;
    
    
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.methods.FileRequestEntity;
    import org.apache.commons.httpclient.methods.PostMethod;
    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 java.io.File;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.util.Map;
    
    
    /**
     * Created by MacGang on 2015/12/27.
     */
    public class HttpSam extends AbstractJavaSamplerClient {
        public static  String label="short_video";
        private SampleResult results;  
        public void setupTest(JavaSamplerContext arg0) { 
        	results = new SampleResult();  
        	}  
        public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        	results.setSampleLabel(label);
            results.sampleStart();
            try {
                Test.execute(javaSamplerContext.getParameter("ip"),javaSamplerContext.getParameter("port"),javaSamplerContext.getParameter("Authorization"),javaSamplerContext.getParameter("path"));
                System.out.println(javaSamplerContext.getParameter("Authorization"));
                results.setSuccessful(true);
            } catch (MalformedURLException e) {
            	results.setSamplerData(e.getMessage());
                e.printStackTrace();
                results.setSuccessful(false);
            }finally{
            	results.sampleEnd();
            }
    
            return results;
        }
        
        public Arguments getDefaultParameters(){
            Arguments args = new Arguments();
            args.addArgument("ip", "192.168.1.11");//设置参数,并赋予默认值0  
            args.addArgument("port", "8080");//设置参数,并赋予默认值0  
            args.addArgument("path", "C:\Users\MacGang\Desktop\url.txt");//设置参数,并赋予默认值0  
            args.addArgument("Authorization", "0");//设置参数,并赋予默认值0 
            
    
            return  args;
        }
        
        public static void main(String[] args) {  
            // TODO Auto-generated method stub  
        	Arguments params = new Arguments();  
        	params.addArgument("ip", "192.168.1.11");//设置参数,并赋予默认值0  
        	params.addArgument("port", "8080");//设置参数,并赋予默认值0  
        	params.addArgument("Authorization", "111");//设置参数,并赋予默认值0
        	params.addArgument("path", "C:\Users\MacGang\Desktop\url.txt");//设置参数,并赋予默认值0  
    
            JavaSamplerContext arg0 = new JavaSamplerContext(params);  
            HttpSam test = new HttpSam();  
            test.setupTest(arg0);
            test.runTest(arg0);  
            System.exit(1);
        }  
    
    
    }
    class Test {
    
        public static void execute(String ip,String port,String auth,String path) throws MalformedURLException {
    
    
            String url = "http://"+ip+":"+port+"/multimedia/upload.json";
            System.out.println(ip+"-->"+port+"-->"+auth);
            HttpClient httpClient = new HttpClient();
            PostMethod post= new PostMethod(url);
            try {
                post.setRequestHeader("Authorization",auth);
                post.setRequestEntity(new FileRequestEntity(new File(path),"application/x-www-form-urlencoded"));
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            try {
                httpClient.executeMethod(post);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    
        }
    }
    

      

  • 相关阅读:
    jquery的内容(html,.text,val)及其属性(attr,prop,data)
    jquery对象的遍历
    jquery的选择器和过滤器
    jquery的基础认知
    解决跨域的四种常见方法
    HTTP中的消息头
    使用js实现ajax加载json文件的组件开发
    IDEA工具第四篇:项目导航Project Navigation下工程包的折叠与展开
    IDEA工具第三篇:启动时报错javax.imageio.IIOException: Can't get input stream from URL!
    IDEA工具第一篇:细节使用-注意事项
  • 原文地址:https://www.cnblogs.com/ggbond1988/p/5081065.html
Copyright © 2020-2023  润新知