首页 技术 正文
技术 2022年11月15日
0 收藏 458 点赞 3,778 浏览 2882 个字
package com.bing.util;import com.bing.constant.ResultModel;
import com.bing.model.Company;
import com.google.common.collect.Lists;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class ss {
public static String sendPost(String url, final Map<String,String> head, final Company paemear) throws Exception{
boolean isSuccess = false;
String str="";
HttpPost post = null;
HttpClient httpClient = new DefaultHttpClient();
// 设置超时时间
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 6000);
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 6000); post = new HttpPost(url);
// 构造消息头 post.setHeader("Content-type", "application/json; charset=utf-8");
for (Map.Entry<String,String> entry : head.entrySet()) {
post.setHeader(entry.getKey(),entry.getValue());
}
// 构建消息实体
// 发送Json格式的数据请求
System.out.println(System.currentTimeMillis());
HttpResponse response = httpClient.execute(post);
System.out.println(System.currentTimeMillis());
// 检验返回码
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode != HttpStatus.SC_OK){
isSuccess = false;
}else{
int retCode = 0;
/**读取服务器返回过来的json字符串数据**/
str = EntityUtils.toString(response.getEntity());
}
if(post != null){
try {
post.releaseConnection();
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return str;
} /**
* 并发量测试
* @param xian 线程池个数
* @param count 每个线程的调用次数
* @param url 调用的 URL
* @param head 头参数
* @param paemear 传入参数
*/
public static void bing(final int xian,final int count, final String url, final Map<String,String> head, final Company paemear){ for(int i = 1; i<xian; i++){
final int m =i;
Thread t = new Thread(new Runnable(){//线程启动
public void run(){
try {
int j = 0;//成功次数
int k = 0; //失败次数
for (int l=1;l<count;l++){
System.out.println(m +"号线程");
String str = ss.sendPost(url,head,paemear);
System.out.println(str);
JSONObject jsonObject=JSONObject.fromObject(str);
ResultModel resultModel=(ResultModel)JSONObject.toBean(jsonObject, ResultModel.class);
if(resultModel.getError()==200){
j++;
}else{
k++;
}
System.out.println(m+"号线程正在调用:"+l+"次");
}
System.out.println(m+"号线程共调应100次 成功:"+j+"失败:"+k);
}catch (Exception e){
System.out.println(m +"号线程查询出错");
}
}
});
t.start();
}
} public static void main(String[] args){
final Map<String,String> head = new HashMap<>();
head.put("token","e7dd5368743457c51405802283d54057ca95f9c73a19fe54");
head.put("terminalType","Android");
Company paemear = new Company();
bing(2,2,"http://192.168.2.203:8080/HouseLizardCloud/Company/addCompany",head,paemear); }
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903