欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java模拟多线程http请求代码分享

程序员文章站 2023-12-16 16:15:10
java模拟http发送请求,第一种是httpurlconnection发送post请求,第二种是使用httpclient模拟post请求, 实例代码: pac...

java模拟http发送请求,第一种是httpurlconnection发送post请求,第二种是使用httpclient模拟post请求,

实例代码:

package test;

import java.util.arraylist;
import java.util.hashmap;
import java.util.list;
import java.util.map;
import java.util.map.entry;
import java.util.concurrent.executorservice;
import java.util.concurrent.executors;

import org.apache.http.httpentity;
import org.apache.http.namevaluepair;
import org.apache.http.client.config.requestconfig;
import org.apache.http.client.entity.urlencodedformentity;
import org.apache.http.client.methods.closeablehttpresponse;
import org.apache.http.client.methods.httppost;
import org.apache.http.impl.client.closeablehttpclient;
import org.apache.http.impl.client.httpclients;
import org.apache.http.message.basicnamevaluepair;
import org.apache.http.util.entityutils;

/**java 模拟测试多线程测试http请求
 * @author wdh
 *
 */

public class servicethreadtest implements runnable{
		
	private string url;
	
	private map<string,object> paramstr;
	
	public servicethreadtest(string url, map<string,object> paramstr) {
		super();
		this.url = url;
		this.paramstr = paramstr;
	}

	
	public string geturl() {
		return url;
	}


	public void seturl(string url) {
		this.url = url;
	}


	public map<string,object> getparamstr() {
		return paramstr;
	}


	public void setparamstr(map<string,object> paramstr) {
		this.paramstr = paramstr;
	}


	@override
	public void run() {
        // http请求实现方式
		closeablehttpclient httpclient = httpclients.createdefault();
		httppost post = new httppost(url);
		requestconfig config = requestconfig.custom().setconnectionrequesttimeout(10000).setconnecttimeout(10000)
				.setsockettimeout(10000).build();
		closeablehttpresponse response = null;
		try {
			list<namevaluepair> params = sethttpnamevalues(paramstr); 
		  httpentity httpentity=new urlencodedformentity(params,"utf-8");
			post.setentity(httpentity);
			post.setconfig(config);
			response = httpclient.execute(post);	
			httpentity entity = response.getentity();
			string content = entityutils.tostring(entity);
			system.out.println("content:" + content);
		} catch (exception e) {
			e.printstacktrace();
		}			
	}	
	
	private list<namevaluepair> sethttpnamevalues(map<string,object> parammap) {
		list<namevaluepair> params = new arraylist<namevaluepair>(); 
		for (entry<string, object> entry:parammap.entryset()){
			 params.add(new basicnamevaluepair(entry.getkey(),entry.getvalue().tostring())); 
		}
	 
		return params;
	}


	public static void main(string[] args) {
       //运用java工具类中线程池
		executorservice pool = executors.newcachedthreadpool();
		for (int i =0;i<2;i++) { //开启俩个线程
			string url = "xxxx";
			map<string,object> paramstr = gethttpparamstr();
			pool.execute(new servicethreadtest(url,paramstr));
		}
	}

	public static map<string,object> gethttpparamstr() {
		map<string, object> param = new hashmap<string, object>();
		param.put("apiversion", 1);
		param.put("appversion", "3.6.2");
		return param;
	}
}

以上就是本次分享的关于java模拟多线程http请求的全部内容,感谢大家对的支持。

上一篇:

下一篇: