平平庸庸

hc for slim。example for hc; file ;log

上一篇 / 下一篇  2010-08-24 15:16:03 / 个人分类:测试工具


/**
 *2010 8 23
 * @author Peter.sun
 */

    private static final Logger logger = Logger.getLogger(MyHttpClient.class);
    private String url;
    private Map<String,String> paramlist=new HashMap();
    private int status;
    private byte[] responseBody;
    private String Body;


        String paramstring;
        if (!this.url.contains("?")){
             paramstring=this.url+"?";
        }else{
             paramstring=this.url+"&";
        }
        //System.out.println(paramstring);
        Iterator iter = paramlist.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            try {
                paramstring += URLEncoder.encode((String)entry.getKey(), "utf-8") + "=" + URLEncoder.encode((String) entry.getValue(), "utf-8") + "&";
            } catch (UnsupportedEncodingException ex) {
                logger.error(ex);
            }
        }
        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod(paramstring);
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler(3, false));
        try {
            client.executeMethod(method);           
          status = client.executeMethod(method);
          if (status != HttpStatus.SC_OK) {
            logger.error("Method failed: " + method.getStatusLine());
          }
          responseBody = method.getResponseBody();
          Body=new String(responseBody, "ISO-8859-1");
        } catch (HttpException e) {
          logger.error("Fatal protocol violation: " + e.getMessage());
        } catch (IOException e) {
          logger.error("Fatal transport error: " + e.getMessage());
        } finally {
          method.releaseConnection();
        }

 


        PostMethod method = new PostMethod(url);
        Iterator iter = paramlist.entrySet().iterator();
        ArrayList<NameValuePair> paramarray=new ArrayList();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            try {
                paramarray.add(new NameValuePair(URLEncoder.encode((String)entry.getKey(), "utf-8"),URLEncoder.encode((String)entry.getValue(), "utf-8")));
            } catch (UnsupportedEncodingException ex) {
                logger.error(ex);
            }
        }
       NameValuePair[] t=new NameValuePair[0];
       NameValuePair[] param=paramarray.toArray(t);
       method.setRequestBody(param);
       method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler(3, false));
       HttpClient client = new HttpClient();
       try {
           client.executeMethod(method);
         status = client.executeMethod(method);
         if (status != HttpStatus.SC_OK) {
           logger.error("Method failed: " + method.getStatusLine());
         }
         responseBody = method.getResponseBody();
          Body=new String(responseBody, "ISO-8859-1");
       } catch (HttpException e) {
         logger.error("Fatal protocol violation: " + e.getMessage());
       } catch (IOException e) {
         logger.error("Fatal transport error: " + e.getMessage());
       } finally {
         method.releaseConnection();
       }

 


        Date dateNow=new Date();
        String[] filepath=filename.split("\\\\");
        String filep="";
        for(int i=0;i<filepath.length-1;i++){
            filep+=filepath[i]+"\\";
        }
        File path=new File(filep);
        if (!path.isDirectory()){
            path.mkdir();
        }
        SimpleDateFormat  dateFormat=new SimpleDateFormat ("yyyy年MM月dd日HH时mm分ss秒");
        String dateNowStr=dateFormat.format(dateNow);
        FileOutputStream fis = new FileOutputStream(filename+dateNowStr+".html",true);
        try {
            fis.write(responseBody);
            } catch (Exception ex) {
            logger.error(ex);
            return false;
        }
        try {
            fis.close();
        } catch (IOException ex) {
            logger.error(ex);
            return false;
        }
        return true;

       byte[] des;
        try {
            des = content.getBytes("utf-8");
        for(int a=0;a<(responseBody.length-des.length+1);a++){
            boolean result=true;
            for(int b=0;b<des.length;b++){
                if (responseBody[a+b]!=des[b]){
                    result=false;
                    break;
                }
            }
            if(result){
                return true;
            }
        }
                    } catch (UnsupportedEncodingException ex) {
            logger.error(ex);
        }
        return false;


TAG:

 

评分:0

我来说两句

Open Toolbar