本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告

戒除浮躁,读好书,交益友


我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:邢红瑞的blog
日志总数:523
评论数量:1142
留言数量:0
访问次数:9693558
建立时间:2004年12月20日




[opensource]HttpClient的使用
原创空间,  软件技术,  电脑与网络

邢红瑞 发表于 2007/6/16 18:33:26

下载页面文件import org.apache.commons.httpclient.*;import org.apache.commons.httpclient.methods.*; public class GetPageExample { public static void main( String[] args ) {   if( args.length() == 0 ) {    System.out.println( "Usage: java GetPageExample URL" );    System.exit( 0 );   }   String url = args[ 0 ];   try {    HttpClient client = new HttpClient();    GetMethod method = new GetMethod( url );    method.setFollowRedirects( true );     // Execute the GET method    int statusCode = client.executeMethod( method );    if( statusCode != -1 ) {      String contents = method.getResponseBodyAsString();      method.releaseConnection();      System.out.println( contents );    }   }   catch( Exception e ) {    e.printStackTrace();   } }}下载文件import org.apache.commons.httpclient.*;import org.apache.commons.httpclient.methods.*; public class GetFileExample { public static void main( String[] args ) {   if( args.length() < 2 ) {    System.out.println( "Usage: java GetFileExample URL filename" );    System.exit( 0 );   }   String url = args[ 0 ];   try {    HttpClient client = new HttpClient();    GetMethod method = new GetMethod( url );    method.setFollowRedirects( true );     // Execute the GET method    int statusCode = client.executeMethod( method );    if( statusCode != -1 ) {      System.out.println( "Reading file" );      InputStream is = method.getResponseBodyAsStream();      BufferedInputStream bis = new BufferedInputStream( is );      FileOutputStream fos = new FileOutputStream( filename );      byte[] bytes = new byte[ 8192 ];      int count = bis.read( bytes );      while( count != -1 && count <= 8192 ) {       System.out.print( "-" );       fos.write( bytes, 0, count );       count = bis.read( bytes );      }      if( count != -1 ) {       fos.write( bytes, 0, count );      }      fos.close();      bis.close();      method.releaseConnection();      System.out.println( "\nDone" );        }   }   catch( Exception e ) {    e.printStackTrace();   } }}


阅读全文(7308) | 回复(1) | 编辑 | 精华
 


回复:HttpClient的使用
原创空间,  软件技术,  电脑与网络

wing5jface(游客)发表评论于2007/9/17 23:40:54

httpclient在实际生产环境中(中国电信的全球眼ADSL环境中取得后台数据第一次连线费时20多秒, 直接使用JDK1.5的HTTPURLCONECTION则二三秒即可返回取得数据,不知各位有没有遇到过?


个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
 


» 1 »

发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.141 second(s), page refreshed 144766673 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号