`
zhykhs
  • 浏览: 60240 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

htmlclient 简单使用方法

阅读更多

1.一个根据动态页路径和编码格式获得html格式文本的方法源代码:

Java代码
   1. // 返回html代码  
   2.     public final static String getHtmlCode(String httpUrl, String ecode) {  
   3.         // 构造HttpClient的实例  
   4.         HttpClient httpClient = new HttpClient();  
   5.         // 创建GET方法的实例  
   6.         GetMethod getMethod = new GetMethod(httpUrl);  
   7.         // 使用系统提供的默认的恢复策略  
   8.         getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,  
   9.                 new DefaultHttpMethodRetryHandler());  
  10.         try {  
  11.             // 执行getMethod  
  12.             int statusCode = httpClient.executeMethod(getMethod);  
  13.             if (statusCode != HttpStatus.SC_OK) {  
  14.                 System.err.println("Method failed: "  
  15.                         + getMethod.getStatusLine());  
  16.             }  
  17.             // 读取内容  
  18.             byte[] responseBody = getMethod.getResponseBody();  
  19.             // 处理内容  
  20.             return new String(responseBody, ecode);  
  21.         } catch (HttpException e) {  
  22.             // 发生致命的异常,可能是协议不对或者返回的内容有问题  
  23.             System.out.println("Please check your provided http address!");  
  24.             e.printStackTrace();  
  25.         } catch (IOException e) {  
  26.             // 发生网络异常  
  27.             e.printStackTrace();  
  28.         } finally {  
  29.             // 释放连接  
  30.             getMethod.releaseConnection();  
  31.         }  
  32.         return null;  
  33.     }  
 2.将html文本写入一个文件中的方法(para1:保存文件名 para2:html文本内容)
   1. public final static void saveHtmlCode(String filePath, String htmlCode) {  
   2.         FileOutputStream fOut = null;  
   3.         OutputStreamWriter out = null;  
   4.         try {  
   5.             fOut = new FileOutputStream(filePath);  
   6.             out = new OutputStreamWriter(fOut, "UTF-8");  
   7.             out.write(htmlCode);  
   8.         } catch (Exception e) {  
   9.             e.printStackTrace();  
  10.         } finally {  
  11.             try {  
  12.                 out.flush();  
  13.             } catch (IOException e) {  
  14.                 e.printStackTrace();  
  15.             }  
  16.             try {  
  17.                 fOut.flush();  
  18.             } catch (IOException e) {  
  19.                 e.printStackTrace();  
  20.             }  
  21.             try {  
  22.                 out.close();  
  23.             } catch (IOException e) {  
  24.                 e.printStackTrace();  
  25.             }  
  26.             try {  
  27.                 fOut.close();  
  28.             } catch (IOException e) {  
  29.                 e.printStackTrace();  
  30.             }  
  31.         }  
  32.     }  
  3.下面是一段伪代码.根据当前时间现在c盘建立要保存文件的文件夹,然后保存文件
# public static void main(String[] args) {  
#         String msgSaveFile = countHtmlFilePath("c://", new Date()) + "/test.html";  
#         String messageHtmlCode = getHtmlCode("test.jsp", "UTF-8");  
#         saveHtmlCode(msgSaveFile, messageHtmlCode);  
#     }  
#       
#     // 获得存储文件的目录  
#     public final static String countHtmlFilePath(String url, Date date) {  
#         SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd-hh");  
#         String time = sim.format(date);  
#         String[] result = time.split("-");  
#         String year = result[0];  
#         String month = result[1];  
#         String day = result[2];  
#         String hour = result[3];   
#         // 首先判断是否有年文件夹  
#         MyFileFilter filter = new TestClass().new MyFileFilter(true);  
#         boolean isExists = filter.isExistsDirectory(url, year + "");  
#         if (isExists) {  
#             if(filter.isExistsDirectory(url + "/" + year, month + "")) {  
#                 if(filter.isExistsDirectory(url + "/" + year + "/" + month, day + "")) {  
#                     if(!filter.isExistsDirectory(url + "/" + year + "/" + month + "/" + hour, hour + "")) {  
#                         new File(url + "/" + year + "/" + month + "/" + day + "/" + hour).mkdir();  
#                     }  
#                 } else {  
#                     new File(url + "/" + year + "/" + month + "/" + day).mkdir();  
#                 }  
#             } else {  
#                 new File(url + "/" + year + "/" + month).mkdir();  
#             }  
#         } else {  
#             new File(url + "/" + year).mkdir();  
#             new File(url + "/" + year + "/" + month).mkdir();  
#             new File(url + "/" + year + "/" + month + "/" + day).mkdir();  
#             new File(url + "/" + year + "/" + month + "/" + day + "/" + hour).mkdir();  
#         }  
#         return url + "/" + year + "/" + month + "/" + day + "/" + hour;  
#     }  
#       
#   
#     class MyFileFilter implements java.io.FileFilter {  
#   
#         private boolean isDirectory;  
#   
#         public MyFileFilter(boolean isDir) {  
#             this.isDirectory = isDir;  
#         }  
#   
#         @Override  
#         public boolean accept(File f) {  
#             if (f.isDirectory() == isDirectory) {  
#                 return true;  
#             } else  
#                 return false;  
#         }  
#           
#         public boolean isExistsDirectory(String path, String dir) {  
#             File file = new File(path);  
#             File[] childFiles = file.listFiles(this);  
#             for (File cFile : childFiles) {  
#                 if(cFile.equals(dir)) {  
#                     return true;  
#                 }  
#             }  
#             return false;  
#         }  
#     } 
 
分享到:
评论

相关推荐

    HtmlClient-3.0.1帮助文档

    HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议 本帮助文档是chm格式,方便使用

    HttpClient类包使用方法

    代码有非常详细地注释,下载java相关包可以直接运行,希望能为将要和打算要学习HtmlClient的人提供一些前期的帮助。

    HTML5QQclient:使用HTML5+ API开发的第三方QQ客户端

    课程设计需要做一个安卓APP,我就想起了HBuilder,回来一看,东西更新了不少,还出现了一个MUI框架,这个框架的存在很大程度上决定了我开始使用这一套框架的理由,它集成了一套非常轻便的UI,而且封装了常用的 ,用...

    爬虫的一个程序

    用HtmlClient4.3和HTMLParse 实现爬虫

    Python实现将HTML转换成doc格式文件的方法示例

    本文实例讲述了Python实现将HTML转换成doc格式文件的方法。分享给大家供大家参考,...class HTMLClient: #获取html网页源码 def GetPage(self, url): #user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows N

    很多java 包 很实用 以及一个UML建模工具

    包括lucene,包,tika包,IKAnalyzer包,UML建模工具,htmlClient包,htmlParser包,外观梅花substance包 ,mysql连接包

    网络爬虫用堆栈队列贮存url

    网络爬虫,初学必看,用链表实现贮存待捉的url队列,用堆栈检查url是否已经捉了。用了HtmlClient和HtmlParser实现。

    基于htmlparser的ed2k搜索存放mysql的Java工具

    基于htmlparser和htmlclient的java搜索ed2k工具,然后你也可以改了搜索其他内容,并按标题和内容存入数据库,web部分还未完善先上传了

Global site tag (gtag.js) - Google Analytics