初探HttpClient——把百度首页扒下来放到本地
闲着没事,找点东西学习,呵呵^^
用HttpClient生成静态页面Demo:
package tests; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; /** * HttpClient生成静态页测试 * * @author Java * @date 2012-02-07 22:55 */ public class TestHttpClient { /** * 测试将百度首页生成到C:\\test\\baidu.html * * @param args */ public static void main(String[] args) { try { writeHTML(getSiteContent("http://www.baidu.com"),"C:\\test\\baidu.html"); System.out.println("OK,生成静态页成功!"); } catch (Exception e) { e.printStackTrace(); } } /** * 根据指定的URL获取网页的内容 * * @param url 访问地址URL * * @return 载有内容的字符串 */ public static String getSiteContent(String url) { HttpClient httpClient = new DefaultHttpClient(); HttpGet request = new HttpGet(url); String content = null; try { HttpResponse response = httpClient.execute(request); HttpEntity entity = response.getEntity(); content = EntityUtils.toString(entity); // System.out.println(EntityUtils.getContentCharSet(entity)); // System.out.println(EntityUtils.getContentMimeType(entity)); // System.out.println(EntityUtils.toString(entity)); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return content; } /** * 将指定的文本写入指定的目录的文件 * * @param content 将要写入的文本 * * @param filePath 文件路径 * * @throws IOException IO异常 * */ public static void writeHTML(String content, String filePath) throws IOException { File file = new File(filePath); File fileParent = file.getParentFile(); if(!fileParent.exists()) { fileParent.mkdirs(); } Writer writer = new FileWriter(file); writer.write(content); writer.close(); } }
相关推荐
Kafka 2020-09-18
Wepe0 2020-10-30
杜倩 2020-10-29
windle 2020-10-29
minerd 2020-10-28
mengzuchao 2020-10-22
Junzizhiai 2020-10-10
bxqybxqy 2020-09-30
风之沙城 2020-09-24
kingszelda 2020-09-22
大唐帝国前营 2020-08-18
yixu0 2020-08-17
TangCuYu 2020-08-15
xiaoboliu00 2020-08-15
songshijiazuaa 2020-08-15
xclxcl 2020-08-03
zmzmmf 2020-08-03
newfarhui 2020-08-03
likesyour 2020-08-01