您的位置:首页 > 理论基础 > 计算机网络

HttpClient的使用实例

2016-04-14 16:04 543 查看
客户端
/**
* 通过探针扫描IP地址段测试
*
*/
public void selectByTanzhen() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
ActionBase actionBase = new ActionBase();
String result =    actionBase.actionBegin(request);
ipmap = new HashMap();
String json = "{success:false}";
String[] end;
String[] start;
String ipstart;
String ipend;
String relativeip;

ipstart = ServletRequestUtils.getStringParameter(request, "ipstart");
ipend = ServletRequestUtils.getStringParameter(request, "ipend");

//创建默认的httpClient实例.
HttpClient httpclient = new DefaultHttpClient();
//创建httppost
HttpPost httppost = new HttpPost("http://localhost:8080/TanzhenAction_scanIp.action");
//创建参数队列
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("ipstart", ipstart));
formparams.add(new BasicNameValuePair("ipend", ipend));
UrlEncodedFormEntity uefEntity;
try {
uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
httppost.setEntity(uefEntity);
System.out.println("executing request " + httppost.getURI());
HttpResponse response1;
response1 = httpclient.execute(httppost);
HttpEntity entity = response1.getEntity();
if (entity != null) {       System.out.println("--------------------------------------");
// System.out.println("Response content: " + EntityUtils.toString(entity,"UTF-8"));
//接收返回的字符串,并解析处理
String str=EntityUtils.toString(entity,"UTF-8");
JSONObject jb=new JSONObject(str);
Iterator iterator=jb.keys();
for(int j = 0; iterator.hasNext(); j++){
ipmap.put(iterator.next().toString(),"true");
}
json = "{success:true}";
System.out.println("--------------------------------------");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
}catch(UnsupportedEncodingException e1) {
e1.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
//关闭连接,释放资源
httpclient.getConnectionManager().shutdown();
}
}

服务端

public void scanIp() throws IOException {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
String json = null;
String str = null;
String[] end;
String[] start;
String relativeip;
String ipstart=request.getParameter("ipstart");
String ipend=request.getParameter("ipend");

start = ipstart.split("\\.");
end = ipend.split("\\.");
relativeip = start[0] + "." + start[1] + "." + start[2] + ".";
int start1=Integer.parseInt(start[3]);
int end1=Integer.parseInt(end[3]);
TanzhenPingThread tanzhenPingThread = new TanzhenPingThread(relativeip,start1,end1);
tanzhenPingThread.startPing();
if(tanzhenmap.size()>0){
str = "{";
Set<Map.Entry> set = tanzhenmap.entrySet();
for (Iterator<Map.Entry> it = set.iterator(); it.hasNext();) {
Map.Entry entry =  it.next();
if(entry.getValue().equals("true")){
str+= "'"+entry.getKey()+"':'"+entry.getKey()+"',";
}
}
if(str.endsWith(",")){
str=str.substring(0,str.length()-1);
}
str +="}";
}
response.setCharacterEncoding("UTF-8");
//把结果字符串返回去
response.getWriter().write(str);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: