//Post Method 例子(模拟登录)
public class PostExample {
private static HttpClient client;
private static Cookie[] cookies;
private static String WEB_SITE = "http://www.xxx.cn";
private static String LOGIN = "http://www.xxx.cn/login.html";
private static String INDEX = "http://www.xxx.cn/index.html";
private static int WEB_PORT = 80;
private static String USER_NAME = "username";
private static String PASSWORD = "password";
static {
client = new HttpClient();
client.getHttpConnectionManager().getParams().setSoTimeout(15000);
client.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
cookies = client.getState().getCookies();
}
private static void testLogin() {
client.getHostConfiguration().setHost(WEB_SITE, WEB_PORT);
GetMethod get = new GetMethod(LOGIN);
GetExample.processGet(client, get, cookies, false, false);
PostMethod post = new PostMethod(LOGIN);
NameValuePair[] params = new NameValuePair[] { new NameValuePair("email", USER_NAME),
new NameValuePair("pass", PASSWORD), new NameValuePair("remember", "1"),
new NameValuePair("goto", "/index.html") };
processPost(client, post, params, cookies, false, false);
Header header = post.getResponseHeader("location");
String url = header.getValue();
if (url.equals(INDEX)) {
System.out.println("登录成功!");
} else {
System.out.println("登录失败,请检查请求参数以及url是否正确...");
return;
}
get = new GetMethod(url);
String result = GetExample.processGet(client, get, cookies, false, true);
//将得到的结果集写到文件里
String filePath = Util.writerFile(result);
//解析html
//ParserExample.parserHtml(filePath);
}
public static String processPost(HttpClient client, PostMethod post, NameValuePair[] params, Cookie[] cookies,
boolean needAppendCookies, boolean needResponse) {
try {
post.setRequestHeader("User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10");
if (params != null && params.length > 0) {
post.setRequestBody(params);
}
if (cookies != null) {
post.setRequestHeader("cookie", cookies.toString());
}
// 设置post方法请求超时为 10秒
post.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 10000);
client.executeMethod(post);
if (needAppendCookies) {
cookies = client.getState().getCookies();
client.getState().addCookies(cookies);
}
if (needResponse) {
return post.getResponseBodyAsString();
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
client.getParams().clear();
post.releaseConnection();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
public static void main(String[] args) {
testLogin();
}
}
//将get,post响应的result写到文件里
public class Util {
public static String writerFile(String result) {
File file = new File("d:\\" + UUID.randomUUID() + ".html");
byte[] bytes = new byte[1024 * 3];
bytes = result.getBytes();
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
fos.write(bytes, 0, bytes.length);
fos.flush();
fos.close();
return file.getAbsolutePath();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
//以htmlparser和正则两种方式解析得到网页上的内容
public class ParserExample {
public static void parserHtml(String filePath) {
parserName(filePath);
parserMemberCount();
}
//解析会员名
private static void parserName(String filePath) {
try {
Parser parser = new Parser(filePath);
parser.setEncoding("gbk");
NodeFilter filter = new AndFilter(new TagNameFilter("a"), new HasAttributeFilter("href",
"http://www.xxx.cn/member/index.html"));
NodeList nodeList = parser.extractAllNodesThatMatch(filter);
if (nodeList != null) {
LinkTag node = (LinkTag) nodeList.elementAt(0);
String name = node.getChildren().elementAt(0).toPlainTextString();
if (name == null) {
System.out.println("解析姓名出错,请检查网页结构是否发生变化");
} else {
System.out.println("姓名:" + name);
}
}
} catch (ParserException e) {
e.printStackTrace();
}
}
//解析网站会员数
private static void parserMemberCount() {
HttpClient client = new HttpClient();
Cookie[] cookies = client.getState().getCookies();
client.getHostConfiguration().setHost("http://www.xxx.cn", 80);
GetMethod get = new GetMethod("http://www.xxx.cn/ajax/memberCount.html");
String result = GetExample.processGet(client, get, cookies, false, true);
Pattern pattern = Pattern.compile("var value=(.*?);");
Matcher matcher = pattern.matcher(result);
if (matcher.find()) {
String memberCount = matcher.group(1);
System.out.println("会员:" + memberCount);
}
}
}
分享到:
相关推荐
HttpClient模拟登录实例,模拟登录网站,获取用户信息,登录后数据,带验证码登录,带解析HTML实例。
例如说我们想知道某个手机号码的归属地而我们自己又没有这样的数据,因此只好借助其他公司已有的网站来完成这个功能,这个时候我们需要向网页提交手机号码并从返回的页面中解析出我们想要的数据来。如果对方仅仅是一...
该工程是通过httpclient模拟请求违章平台并解析网页反馈信息,其中包含简单验证码的识别处理,网页数据抓取,httpclient模拟用户登录的技术。
有些时候会遇到请求http,获取相应的json或者xml数据,今天整理了一点httpClient请求的示例代码。主要是根据相应的http地址,以及相关秘钥,通过HttpClient去发起请求,获取数据然后后台取解析json或者xml。
其主要功能是访问网页、提取数据并存储,以便后续分析或展示。爬虫通常由搜索引擎、数据挖掘工具、监测系统等应用于网络数据抓取的场景。 爬虫的工作流程包括以下几个关键步骤: URL收集: 爬虫从一个或多个初始...
其主要功能是访问网页、提取数据并存储,以便后续分析或展示。爬虫通常由搜索引擎、数据挖掘工具、监测系统等应用于网络数据抓取的场景。 爬虫的工作流程包括以下几个关键步骤: URL收集: 爬虫从一个或多个初始...
其主要功能是访问网页、提取数据并存储,以便后续分析或展示。爬虫通常由搜索引擎、数据挖掘工具、监测系统等应用于网络数据抓取的场景。 爬虫的工作流程包括以下几个关键步骤: URL收集: 爬虫从一个或多个初始...
其主要功能是访问网页、提取数据并存储,以便后续分析或展示。爬虫通常由搜索引擎、数据挖掘工具、监测系统等应用于网络数据抓取的场景。 爬虫的工作流程包括以下几个关键步骤: URL收集: 爬虫从一个或多个初始...
java实现在线抓取网易云用户信息,支持在线抓取网易云用户信息,用户头像,昵称,电话等等;抓取网易云音乐热门歌手及其歌曲、专辑等...使用HttpClient和Jsoup进行模拟请求并对网页进行解析;使用mybatis作为数据访问
其主要功能是访问网页、提取数据并存储,以便后续分析或展示。爬虫通常由搜索引擎、数据挖掘工具、监测系统等应用于网络数据抓取的场景。 爬虫的工作流程包括以下几个关键步骤: URL收集: 爬虫从一个或多个初始...
Java爬虫是通过Java语言编写的网络爬虫程序,用于自动化地浏览和抓取互联网上的数据,并将数据进行处理和保存。 为了编写高效且不容易被检测出来的Java爬虫,通常需要掌握Java语言以及与之相关的库和框架,如Jsoup...
1)HttpClient模拟浏览器发送请求(目前只支持get请求) 2)集成Jsoup解析器,用于HTML页面解析 3)使用Redis对URL快速去重 4)使用slf4j作为日志门面 ###项目功能: 1)支持个性化性质 2)支持定时任务增量爬取数据...
在解析网页信息的时候,需要登录后才能访问,所以使用httpclient模拟登录,然后把cookie保存下来,以供下一次访问使用,这时就需要持久化cookie中的内容。 在之前先科普一下基础知识: 什么是Cookies? Cookies是...
6个目标文件,EJB来模拟银行ATM机的流程及操作:获取系统属性,初始化JNDI,取得Home对象的引用,创建EJB对象,并将当前的计数器初始化,调用每一个EJB对象的count()方法,保证Bean正常被激活和钝化,EJB对象是用...
6个目标文件,EJB来模拟银行ATM机的流程及操作:获取系统属性,初始化JNDI,取得Home对象的引用,创建EJB对象,并将当前的计数器初始化,调用每一个EJB对象的count()方法,保证Bean正常被激活和钝化,EJB对象是用...
|--xml文件的pull解析与序列化写入 |--xml的封装序列化 |--任务循环之只在Activity显示时执行 |--修改文件的最后修改时间 |--偏好设置(回显) |--内存优化之各种方法 |--内容提供者之短信的序列化对象读写 |--内容...