@RequestMapping("getpage")
public ModelAndView admin_checkurl(HttpServletRequest request) throws IOException {
ModelAndView mav = new ModelAndView();
HttpClientBuilder builder = HttpClients.custom(); //我们可以使用一个Builder来设置UA字段,然后再创建HttpClient对象
builder.setUserAgent("Mozilla/5.0(Windows;U;Windows NT 5.1;en-US;rv:0.9.4)");//对照UA字串的标准格式理解一下每部分的意思
final CloseableHttpClient httpClient = builder.build();
String targetUrl = "";
//targetUrl = request.getParameter("url");
targetUrl = "http://www.ip138.com/";
targetUrl = targetUrl.replace(" ", "");//去除所有的空格
HttpGet httpget = new HttpGet(targetUrl);
CloseableHttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println(entity);
String page = EntityUtils.toString(entity, "GBK");//GBK解决中文乱码
System.out.println(page);
// 解析网页 得到文档对象
Document doc = Jsoup.parse(page);
response.close();
httpClient.close();
mav.addObject("page", page);
mav.setViewName("jsp/check_report");
return mav;
}