Apache并发请求数及其TCP连接状态
2015-07-28 12:34
495 查看
package cn.wemart.httppost;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
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.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.dom4j.DocumentException;
public class ExecuteGet {
/**
* 组装请求参数
* @param keyVlaueList
*/
public static Map<String ,String> setPostPara(String[][] keyVlaueList) throws DocumentException{
Map<String ,String> map = new HashMap<String ,String>();
for(int i = 0;i<keyVlaueList.length;i++)
{
map.put(keyVlaueList[i][0],keyVlaueList[i][1]);
}
return map;
}
/**
* 获取接口返回字符串
* @param url
* @throws DocumentException
* @throws URISyntaxException
*/
public static String getGetMethodResponse(String url,String[][] keyVlaueList) {
String xmlStr =null;
String Str;
try {
//把键值对转换成JSON格式串
Map<String, String> postMap;
postMap = setPostPara(keyVlaueList);
JSONObject object = JSONObject.fromObject(postMap);
List<NameValuePair> postPara = new ArrayList<NameValuePair>();
postPara.add(new BasicNameValuePair("para",object.toString()));
System.out.println("+++++para="+object.toString()+"+++++");
//发送转码过的字符串,发送
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet(url);
String sendstr = EntityUtils.toString(new UrlEncodedFormEntity(postPara,HTTP.UTF_8));
httpget.setURI(new URI(httpget.getURI().toString() + "?" + sendstr));
CloseableHttpResponse response = httpclient.execute(httpget);
//处理response的中文编码
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),"UTF-8"));
StringBuffer sb = new StringBuffer();
while ((Str = reader.readLine()) != null) {
sb.append(Str).append("\n");
}
xmlStr = sb.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
return xmlStr;
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
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.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.dom4j.DocumentException;
public class ExecuteGet {
/**
* 组装请求参数
* @param keyVlaueList
*/
public static Map<String ,String> setPostPara(String[][] keyVlaueList) throws DocumentException{
Map<String ,String> map = new HashMap<String ,String>();
for(int i = 0;i<keyVlaueList.length;i++)
{
map.put(keyVlaueList[i][0],keyVlaueList[i][1]);
}
return map;
}
/**
* 获取接口返回字符串
* @param url
* @throws DocumentException
* @throws URISyntaxException
*/
public static String getGetMethodResponse(String url,String[][] keyVlaueList) {
String xmlStr =null;
String Str;
try {
//把键值对转换成JSON格式串
Map<String, String> postMap;
postMap = setPostPara(keyVlaueList);
JSONObject object = JSONObject.fromObject(postMap);
List<NameValuePair> postPara = new ArrayList<NameValuePair>();
postPara.add(new BasicNameValuePair("para",object.toString()));
System.out.println("+++++para="+object.toString()+"+++++");
//发送转码过的字符串,发送
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet(url);
String sendstr = EntityUtils.toString(new UrlEncodedFormEntity(postPara,HTTP.UTF_8));
httpget.setURI(new URI(httpget.getURI().toString() + "?" + sendstr));
CloseableHttpResponse response = httpclient.execute(httpget);
//处理response的中文编码
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),"UTF-8"));
StringBuffer sb = new StringBuffer();
while ((Str = reader.readLine()) != null) {
sb.append(Str).append("\n");
}
xmlStr = sb.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
return xmlStr;
}
}
相关文章推荐
- [Android] WebView内的本地网页,使用XMLHttpRequest读取本地档案
- 查看及测试网络
- HTTP协议中POST、GET、HEAD、PUT等请求方法以及一些常见错误 #Reprinted#
- 网络远程教育实施方案交流之(三)——服务器架构及视频存储方案
- http://blog.sina.com.cn/s/blog_4c0e8aa20100ung8.html
- WebService或HTTP服务端接收请求转发消息到另一个服务端-实现思路
- 人工神经网络基础与研究内容
- uboot启动完成后对网络环境的配置
- python实现单隐层神经网络基本模型 推荐
- 网络编程系列之二 互斥量
- tomcat7 服务器配置HTTPS安全协议
- 网络爬虫基本原理(一)
- 网络爬虫基本原理二
- 1149-PIGS(网络流建模,缩点)
- 自组织神经网络的实现
- Nmap新增脚本http-svn-enum和http-svn-info
- Linux网络编程select模型的实现
- ProtoBuf
- https 对接非对称密钥加密 MD5及数字签名相关
- TCP and UDP Small Servers