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); }
相关文章推荐
- TCP连接的11种状态变迁
- AFNetworking配置网络请求
- linux网络编程之shutdown() 与 close()函数详解
- 实现多路网络摄像头连接DM8168
- 在与SQLServer建立连接时出现与网络相关的或特定于实例的错误
- Android 主流网络请求框架 Volley
- 网页浏览远程网络视频
- 解析网络视频真实地址
- TCP协议简单总结
- android6.0Okhttp网络请求
- http协议详解
- Linux网络编程--select,poll和epoll的区别
- Android Studio找不到org.apache.http中的类
- (转)对Http协议的长连接和短连接新的认识
- socket通讯原理以及tcp、ip三次握手机制分析
- HTTP in iOS你看我就够
- xdoe7 Transport security has blocked a cleartext HTTP
- Tomcat7启动报错:Exception in thread "http-apr-8080-exec-6" java.lang.OutOfMemoryError: PermGen space
- 神经网络入门--学习资源
- HTTP的长短连接、长短轮询的区别(转载)