HttpClient的使用实例

客户端
/**
 * 通过探针扫描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);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值