package com.xinhuanet.cloudDesk.controller;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.htmlparser.Parser;
import org.htmlparser.visitors.HtmlPage;
public class R {
public static void main(String[] args) throws Exception {
HttpClient httpClient = new HttpClient();
httpClient.getHostConfiguration().setProxy("202.84.17.41", 8080);
HttpConnectionManager httpConnManager = httpClient
.getHttpConnectionManager();
if (httpConnManager != null) {
HttpConnectionManagerParams mgrParams = new HttpConnectionManagerParams();
mgrParams.setSoTimeout(20000000);
mgrParams.setTcpNoDelay(true);
mgrParams.setConnectionTimeout(20000000);
mgrParams.setLinger(0);
mgrParams.setStaleCheckingEnabled(false);
httpConnManager.setParams(mgrParams);
}
String url = "http://www.poetry4cn.com";
GetMethod methodGet = new GetMethod(url);
httpClient.executeMethod(methodGet);
String charset = getCharSet(new String(methodGet.getResponseBody()));
System.out.println("getCharSet:" + charset);
String responseGet = new String(methodGet.getResponseBody(), charset);
System.out.println(responseGet);
Parser myParser = Parser.createParser(responseGet.toString(), charset);
HtmlPage visitor = new HtmlPage(myParser);
myParser.visitAllNodesWith(visitor);
String textInPage = visitor.getTitle();
System.out.println("title:" + textInPage);
}
public static String getCharSet(String content) {
// String regex = ".*charset=([^;]*).*";
String regex = "<meta.+?charset=[^\\w]?([-\\w]+)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(content);
if (matcher.find())
return matcher.group(1);
else
return null;
}
}
分享到:
相关推荐
java进行爬虫开发必备的两个开源包,包括HttpClient.jar和HttpParser.jar
httpClient和htmlparse获取网页数据使用jar
通过httpclient post去获取,response返回码是302,返回的code放在header的Location中。 请求的时候client_id,response_type,redirect_uri,state拼接在url后面,account和password放在body表单(x-...
从网络获取数据和图片,用gson解析并用listview展示
使用 HttpClient 和 HtmlParser 实现简易网络爬虫
使用HttpClient必须的jar包 使用HttpClient必须的jar包 使用HttpClient必须的jar包
...httpclient.HttpParser org.apache.commons.httpclient....
本例子是一个使用HttpClient和URLConnection获取网页html内容的小例子,获取到的源码不解析直接显示,技术比较简单,需要的朋友可以下载研究一下,项目编码GBK默认编译版本2.3.3
HttpClient已经应用在很多的项目中,比如Apache Jakarta上很著名的另外两个开源项目Cactus和HTMLUnit都使用了HttpClient。 下载地址: http://hc.apache.org/downloads.cgi 1.2特性 1. 基于标准、纯净的java语言。...
httpClient完整封装获取网页信息、数据的代码+httpclient.jar
通过httpclient的方式获取返回的文本,也可以访问某些支持http方式访问的接口,返回XML并解析
android HttpClient 获取网络图片 实例
HttpClient是个很不错的开源框架(org.appache.http),封装了访问http的请求头,参数,内容体,响应等等,使用起来更方面更强大。 HttpURLConnection是java的标准类,可以实现简单的基于URL请求、响应功能,什么都...
使用HttpClient获取网页html源代码获取到的源码不解析直接显示,技术比较简单,需要的朋友可以下载研究一下,项目编码GBK默认编译版本2.3.3。
本例子是通过httpClient 不断的去电信的网站查询可选手机新号 , 每次查出来的数据保存到map , 到一定数量以后 , 保存在本地文件中 , 测试类可以直接运行.
try(CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(url); StringEntity stringEntity = new StringEntity(params, Charset.forName("UTF-8")); ...
android httpclient 访问服务器 获取json数据,里面有需要载入的外部jar包,添加源代码后再加入外部包就可。
这个代码的作用是使用HttpClient发送POST请求,并获取响应内容。具体来说,它创建了一个HttpClient实例和一个HttpPost对象,设置了请求的URL、请求头和请求体,然后执行请求并获取响应。最后,它将响应内容输出到...
使用HttpClient登录网易邮箱 博文链接:https://bps.iteye.com/blog/136231
本例子是一个使用HttpClient和URLConnection获取网页html内容的小例子,获取到的源码不解析直接显示,技术比较简单,需要的朋友可以下载研究一下,项目编码GBK默认编译版本2.3.3