« | August 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 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(); } }} |
|
回复:HttpClient的使用 原创空间, 软件技术, 电脑与网络
wing5jface(游客)发表评论于2007/9/17 23:40:54 |
httpclient在实际生产环境中(中国电信的全球眼ADSL环境中取得后台数据第一次连线费时20多秒,
直接使用JDK1.5的HTTPURLCONECTION则二三秒即可返回取得数据,不知各位有没有遇到过? |
|
» 1 »
|