首页 > 代码库 > 页面静态化-http get请求获取页面内容代码
页面静态化-http get请求获取页面内容代码
1 import org.apache.http.HttpEntity; 2 import org.apache.http.HttpException; 3 import org.apache.http.HttpResponse; 4 import org.apache.http.client.HttpClient; 5 import org.apache.http.client.methods.HttpGet; 6 import org.apache.http.client.methods.HttpRequestBase; 7 import org.apache.http.impl.client.HttpClients; 8 import org.apache.http.protocol.BasicHttpContext; 9 import org.apache.http.protocol.HttpContext;10 import org.apache.http.util.EntityUtils;11 12 /**13 * 页面静态化,通过http get请求获取页面内容14 * 注意:要引入common-logging、httpcore、httpclient的jar包15 */16 public class PageStatic17 {18 public static void main(String[] args)19 {20 HttpClient client = HttpClients.createDefault();21 String url = "http://fanyi.youdao.com/";22 HttpRequestBase http = new HttpGet(url);23 HttpContext context = new BasicHttpContext();24 try25 {26 HttpResponse response = client.execute(http, context);27 int statusCode = response.getStatusLine().getStatusCode();28 29 switch (statusCode)30 {31 case 200:32 case 400:// 业务异常33 break;34 default:35 throw new HttpException(url + " Status Code:" + statusCode);36 }37 38 HttpEntity entity = response.getEntity();39 String reStr = EntityUtils.toString(entity);40 System.out.println(reStr);41 }42 catch (Exception e)43 {44 System.out.println(e.toString());45 }46 }47 48 }
页面静态化-http get请求获取页面内容代码
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。