您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)java如何模擬多線程http請(qǐng)求代碼,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
java模擬http發(fā)送請(qǐng)求,第一種是HttpURLConnection發(fā)送post請(qǐng)求,第二種是使用httpclient模擬post請(qǐng)求,
實(shí)例代碼:
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 模擬測(cè)試多線程測(cè)試http請(qǐng)求 * @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請(qǐng)求實(shí)現(xiàn)方式 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) { //運(yùn)用java工具類中線程池 ExecutorService pool = Executors.newCachedThreadPool(); for (int i =0;i<2;i++) { //開啟倆個(gè)線程 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; } }
關(guān)于“java如何模擬多線程http請(qǐng)求代碼”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。