- 浏览: 562489 次
- 来自: -
博客专栏
-
libgdx 游戏开发
浏览量:12273
文章分类
- 全部博客 (171)
- OS (1)
- JavaScript (13)
- Struts (2)
- Regular Expression (1)
- Java (14)
- HTML (4)
- XML (1)
- Non-Relational Database (2)
- Miscellaneous (7)
- Lotus Notes (8)
- Algorithm (3)
- Web Analytics (6)
- Web (8)
- Perl (3)
- PHP (3)
- C & C++ (1)
- Shell (7)
- Google (1)
- Android (31)
- iPhone (1)
- SQL (1)
- HTML5 (3)
- jQuery (6)
- CSS (6)
- PostgreSQL (1)
- Design Patterns (1)
- Excel (1)
- Magento (4)
- jMeter (3)
- SEO (1)
- libgdx (5)
- Software (4)
- App (1)
- Game (1)
- Gradle (1)
- Linux (16)
- Ubuntu (4)
- Docker (2)
- Spring (2)
- Other (3)
- Directory Server (1)
- CentOS (1)
- Python (1)
- VCS (3)
- Database (1)
- Open Source (1)
最新评论
-
ls0609:
赞一个,支持下博主。
[原创] Android ListView 在右上角添加三角形图标和文字 -
love297:
不让别人商用,自己先商用起来了。
手机游戏开发展示 -
a851206:
你的有些类是哪里来的?我想研究一下你的程序,可是有些类没有代码 ...
[原创] Google Custom Search & Yahoo Boss Search | Web Search API 使用 -
ypppk:
BitmapFactory.Options options = ...
[原创] 连载 1 - 深入讨论 Android 关于高效显示图片的问题 - 如何高效的加载大位图 -
笑遍世界:
我也遇到了,弄清了其中原因,可参考我的博客:http://sm ...
[原创] 使用 jMeter 登录 Wordpress
在网上查了好长时间,发现httpclient的例子都是讲httpclient 3.X的。httpclient 4.0的例子简直是凤毛麟角。只好自己上官网仔细了查了下文档和官网示例,受益匪浅啊!
由于前段时间对“开心网”的“开心大亨”很感兴趣,但是每隔一段时间就需要看一下最新的价格变化,很是麻烦,由于我就想起用httpclient写个程序,自动查看最新的价格,并和前次的价格做比较,好让我可以选择利润最大的进行投资。
程序用到的包如下,为了附件的瘦身,请大家自己下载啊,附件中是没有这些lib的啊。
commons-collections-3.2.1.jar
commons-configuration-1.6.jar
commons-io-1.4.jar
commons-lang-2.4.jar
commons-logging-1.1.1.jar
httpclient-4.0.jar
httpcore-4.0.1.jar
由于只是例子,而且httpclient4.0的结构变化很大,自己对其理解也不是很深,此程序只是个入门程序,如果有高手在的话,希望大家可以提出宝贵的意见。
程序写的不是很美观,但意在让大家明白如何简单的使用httpclient4.0,所以没有对程序做任何的重构的整理,望大家多多谅解啊。
程序代码如下:
主程序: KaiXin001.java
访问开心大亨组件的程序:KaiXin_Rich.java
由于前段时间对“开心网”的“开心大亨”很感兴趣,但是每隔一段时间就需要看一下最新的价格变化,很是麻烦,由于我就想起用httpclient写个程序,自动查看最新的价格,并和前次的价格做比较,好让我可以选择利润最大的进行投资。
程序用到的包如下,为了附件的瘦身,请大家自己下载啊,附件中是没有这些lib的啊。
commons-collections-3.2.1.jar
commons-configuration-1.6.jar
commons-io-1.4.jar
commons-lang-2.4.jar
commons-logging-1.1.1.jar
httpclient-4.0.jar
httpcore-4.0.1.jar
由于只是例子,而且httpclient4.0的结构变化很大,自己对其理解也不是很深,此程序只是个入门程序,如果有高手在的话,希望大家可以提出宝贵的意见。
程序写的不是很美观,但意在让大家明白如何简单的使用httpclient4.0,所以没有对程序做任何的重构的整理,望大家多多谅解啊。
程序代码如下:
主程序: KaiXin001.java
/* * 2009/12/21 * Author: Yuan Hongzhi */ import java.io.File; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.apache.commons.io.FileUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.cookie.Cookie; import org.apache.http.entity.BufferedHttpEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; public class KaiXin001 { public InputStream getResourceAsStream(String filename) throws Exception { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); InputStream in = null; if (classLoader != null) { in = classLoader.getResourceAsStream(filename); } if (in == null) { in = ClassLoader.getSystemResourceAsStream(filename); } if (in == null) { throw new Exception("Can't find resource file: " + filename); } else { return in; } } public void writeToFile(String file, HttpEntity entity) throws Exception { writeToFile(file, EntityUtils.toString(entity)); } public void writeToFile(String file, String data) throws Exception { FileUtils.writeStringToFile(new File(file), data, "UTF-8"); } public String getContent(HttpEntity entity) throws Exception { if (entity != null) { entity = new BufferedHttpEntity(entity); long len = entity.getContentLength(); System.out.println("Length: " + len); System.out.println("====================\r\n"); return EntityUtils.toString(entity, "UTF-8"); } else { System.out.println("entity is null."); return null; } } public void setCookie(DefaultHttpClient httpclient, List<Cookie> cookies) { if (cookies.isEmpty()) { System.out.println("Cookie is empty."); return; } else { for (int i = 0; i < cookies.size(); i++) { System.out.println((i + 1) + " - " + cookies.get(i).toString()); httpclient.getCookieStore().addCookie(cookies.get(i)); } System.out.println(); } } // "开心网其它组件URL如下,大家可以添加上自己喜欢的组件URL。" // "http://www.kaixin001.com/!slave/index.php", "朋友买卖" // "http://www.kaixin001.com/!parking/index.php", "争车位" // "http://www.kaixin001.com/!house/index.php?_lgmode=pri", "买房子" // "http://www.kaixin001.com/!house/index.php?_lgmode=pri&t=49" // "http://www.kaixin001.com/!house/garden/index.php","花园" // "http://www.kaixin001.com/!rich/market.php", "超级大亨" public String enterComponentContent(String url, String componentName, DefaultHttpClient httpclient, List<Cookie> cookies, HttpResponse response, HttpEntity entity) throws Exception { System.out.println("--- Enter: " + componentName + " ---"); System.out.println("--- Url: " + url + " ---"); setCookie(httpclient, cookies); HttpGet httpget = new HttpGet(url); response = httpclient.execute(httpget); entity = response.getEntity(); return getContent(entity); } public void showResponseStatus(HttpResponse response) { // System.out.println(response.getProtocolVersion()); // System.out.println(response.getStatusLine().getStatusCode()); // System.out.println(response.getStatusLine().getReasonPhrase()); System.out.println(response.getStatusLine().toString()); System.out.println("-------------------------\r\n"); } public static void main(String[] args) throws Exception { KaiXin001 kx = new KaiXin001(); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost httpost = new HttpPost("http://www.kaixin001.com/login/login.php"); List<NameValuePair> qparams = new ArrayList<NameValuePair>(); qparams.add(new BasicNameValuePair("email", "email")); qparams.add(new BasicNameValuePair("password", "password")); httpost.setEntity(new UrlEncodedFormEntity(qparams, HTTP.UTF_8)); HttpResponse response = httpclient.execute(httpost); kx.showResponseStatus(response); // HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); kx.getContent(entity); // Login List<Cookie> cookies = httpclient.getCookieStore().getCookies(); System.out.println("Post logon cookies:"); kx.setCookie(httpclient, cookies); // Redirect to home page String homepage = "http://www.kaixin001.com/home/"; String content = null; content = kx.enterComponentContent(homepage, "Home page", httpclient, cookies, response, entity); // Component String componet = "http://www.kaixin001.com/!rich/market.php"; content = kx.enterComponentContent(componet, "Component", httpclient, cookies, response, entity); // -------------------------------------------- kx.writeToFile("c:/kaixin.html", content); // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
访问开心大亨组件的程序:KaiXin_Rich.java
/* * 2009/12/21 * Author: Yuan Hongzhi */ import java.awt.Toolkit; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.collections.keyvalue.DefaultKeyValue; import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.cookie.Cookie; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; public class KaiXin_Rich extends KaiXin001 { public static List<DefaultKeyValue> curList = new ArrayList<DefaultKeyValue>(); public static List<DefaultKeyValue> preList = new ArrayList<DefaultKeyValue>(); public void copyList(List<DefaultKeyValue> src, List<DefaultKeyValue> des) { for (DefaultKeyValue dkv : src) { DefaultKeyValue kv = new DefaultKeyValue(dkv.getKey(), dkv.getValue()); des.add(kv); } } public Double subNumber(Object string) { String rex = "([\\d\\.]*)"; Pattern pattern = Pattern.compile(rex); Matcher match = pattern.matcher(string.toString()); if (match.find()) { return Double.valueOf(match.group()); } else return -1.0; } public String extractNoneAscii(Object string) { String rex = "([^\\x00-\\xff]+)"; Pattern pattern = Pattern.compile(rex); Matcher match = pattern.matcher(string.toString()); if (match.find()) { return match.group(); } else return ""; } public static void main(String[] args) throws Exception { KaiXin_Rich kx = new KaiXin_Rich(); PropertiesConfiguration config = new PropertiesConfiguration(); config.load(kx.getResourceAsStream("properties/kaixin001.properties"), "UTF-8"); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost httpost = new HttpPost("http://www.kaixin001.com/login/login.php"); List<NameValuePair> qparams = new ArrayList<NameValuePair>(); qparams.add(new BasicNameValuePair("email", config.getString("email"))); qparams .add(new BasicNameValuePair("password", config.getString("password"))); httpost.setEntity(new UrlEncodedFormEntity(qparams, HTTP.UTF_8)); HttpResponse response = httpclient.execute(httpost); kx.showResponseStatus(response); // HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); kx.getContent(entity); // Login List<Cookie> cookies = httpclient.getCookieStore().getCookies(); System.out.println("Post logon cookies:"); kx.setCookie(httpclient, cookies); // Redirect to home page String homepage = "http://www.kaixin001.com/home/"; String content = null; content = kx.enterComponentContent(homepage, "Home page", httpclient, cookies, response, entity); int loopTime = config.getInt("times"); long sleepTime = config.getLong("period") * 60; for (int j = 0; j < loopTime; j++) { // Component String componet = config.getString("url"); content = kx.enterComponentContent(componet, "Component", httpclient, cookies, response, entity); int beginIndex = content.indexOf(config.getString("startRex")); int endIndex = content.lastIndexOf(config.getString("endRex")); content = content.substring(beginIndex, endIndex); // -------------------------------------------- String rex = config.getString("displayKeyRex"); Pattern pattern = Pattern.compile(rex, Pattern.MULTILINE); Matcher match = pattern.matcher(content); List<String> keys = new ArrayList<String>(); String matched = null; while (match.find()) { matched = match.group(); keys.add(matched); } rex = config.getString("displayValueRex"); pattern = Pattern.compile(rex, Pattern.MULTILINE); match = pattern.matcher(content); List<String> values = new ArrayList<String>(); while (match.find()) { matched = match.group(); values.add(matched); } DefaultKeyValue kv = null; for (int i = 0; i < keys.size(); i++) { kv = new DefaultKeyValue(keys.get(i), values.get(i)); curList.add(kv); } String rtnString = ""; String eachLine = null; for (DefaultKeyValue dkv : curList) { eachLine = dkv.getKey() + "\t:" + dkv.getValue(); rtnString += eachLine; rtnString += "\r\n"; System.out.println(eachLine); } System.out.println("\r\n===== Changed =====\r\n"); String allPriceString = "===== Changed =====\r\n\r\n"; StringBuffer price = null; double changed = 0; boolean beepFlag = false; for (int i = 0; i < curList.size() && curList.size() == preList.size(); i++) { double curMoney = kx.subNumber(curList.get(i).getValue()); double preMoney = kx.subNumber(preList.get(i).getValue()); if ((curMoney - preMoney) > 1E-5) { changed = curMoney - preMoney; DecimalFormat format = new DecimalFormat("###.##"); String number = format.format(changed); price = new StringBuffer(); price.append(curList.get(i).getKey()); price.append("\t: "); price.append(preList.get(i).getValue()); price.append(" -> "); price.append(curList.get(i).getValue()); price.append("\t"); price.append(" Changed: "); price.append(number); price.append(kx.extractNoneAscii(curList.get(i).getValue())); System.out.println(price); allPriceString = allPriceString + price.toString() + "\r\n"; beepFlag = true; } } if (beepFlag) { Toolkit.getDefaultToolkit().beep(); } preList.clear(); // System.out.println(content); String dateTime = DateUtils .getSystemDateTime(DateTimePattern.DATE_TIME_LONG_UNFORMAT); kx.writeToFile("c:/kaixin001/price_" + dateTime + ".txt", rtnString + "\r\n" + allPriceString); // kx.writeToFile("c:/kaixin.html", content); kx.copyList(curList, preList); curList.clear(); Thread.sleep(sleepTime * 1000); } // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
- kaixin_src.zip (5.4 KB)
- 下载次数: 180
发表评论
-
[转] remove on List created by Arrays.asList throws UnsupportedOperationException
2017-08-02 09:30 532原文地址:https://stackoverflow.com/ ... -
[转] JAVA 遍历文件夹下的所有文件(递归调用和非递归调用)
2017-02-04 14:26 2599原文地址: http://www.cnblogs.com/az ... -
java中静态代码块的用法 static用法详解
2013-05-15 11:24 0http://www.cnblogs.com/panjun-D ... -
[原创] Java List/Array 去重
2013-03-06 13:03 2165【说明】 刚刚才发现有这么多的回复!没想到这篇文章会这么容 ... -
[转] Java 随机数 - 生成指定范围的随机数
2012-11-16 16:42 1520大家可以参看原文: http://stackoverflow. ... -
[转] ListResourceBundle doesn't run inside package
2012-11-16 09:29 1044原文地址: http://www.eclipse.org/fo ... -
[转] Java版 中国农历算法 中国农历生成算法
2012-08-06 14:16 1504/** * 實用於公曆 1901 年至 2100 年之 ... -
[原创] Google Custom Search & Yahoo Boss Search | Web Search API 使用
2011-01-26 16:28 2751引用本文时,请标明本文地址 话外音:前几天上网找资料 ... -
[原创] Java 天气预告 - htmlparser2.0 + httpclient4.0
2011-01-11 11:16 2613Java控制台版 - 天气预告 前段时间,没事儿的时 ... -
(Original) Nine Palace - Java Implementation - (原创) Java 九宫图 实现
2010-05-26 17:14 1363Some days ago, my girlfrien ... -
原创: Java 删除List中的多余元素 (List 去重)
2010-05-24 16:02 6399如何去除List中重复的数据,可以使用如下的方法。 ... -
Java String 一个简单例子的深思 (对象比较 值相等 相关问题)
2010-05-12 16:19 2042只要是学习过Java的人, ... -
Java 反射例子详解
2010-04-08 09:02 1207Java编程 的动态性,第 2部分: 引入反射 http:// ... -
JAVA 反射例子
2010-03-29 09:35 1035Java Reflection (JAVA反射) ...
相关推荐
赠送jar包:httpclient-4.5.13.jar; 赠送原API文档:httpclient-4.5.13-javadoc.jar; 赠送源代码:httpclient-4.5.13-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.13.pom; 包含翻译后的API文档:...
赠送jar包:httpclient-4.5.12.jar; 赠送原API文档:httpclient-4.5.12-javadoc.jar; 赠送源代码:httpclient-4.5.12-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.12.pom; 包含翻译后的API文档:...
赠送jar包:httpclient-4.5.6.jar; 赠送原API文档:httpclient-4.5.6-javadoc.jar; 赠送源代码:httpclient-4.5.6-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.6.pom; 包含翻译后的API文档:httpclient...
赠送jar包:httpclient-4.5.5.jar; 赠送原API文档:httpclient-4.5.5-javadoc.jar; 赠送源代码:httpclient-4.5.5-sources.jar; 包含翻译后的API文档:httpclient-4.5.5-javadoc-API文档-中文(简体)版.zip ...
赠送jar包:httpclient-4.5.13.jar; 赠送原API文档:httpclient-4.5.13-javadoc.jar; 赠送源代码:httpclient-4.5.13-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.13.pom; 包含翻译后的API文档:...
httpclient-4.0-beta2.jarhttpclient-4.0-beta2.jarhttpclient-4.0-beta2.jarhttpclient-4.0-beta2.jarhttpclient-4.0-beta2.jarhttpclient-4.0-beta2.jarhttpclient-4.0-beta2.jarhttpclient-4.0-beta2.jar
httpclient-4.0-beta1.jar
赠送jar包:httpclient-4.5.6.jar; 赠送原API文档:httpclient-4.5.6-javadoc.jar; 赠送源代码:httpclient-4.5.6-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.6.pom; 包含翻译后的API文档:httpclient...
赠送jar包:httpclient-4.4.1.jar; 赠送原API文档:httpclient-4.4.1-javadoc.jar; 赠送源代码:httpclient-4.4.1-sources.jar; 赠送Maven依赖信息文件:httpclient-4.4.1.pom; 包含翻译后的API文档:httpclient...
赠送jar包:httpclient-4.5.12.jar; 赠送原API文档:httpclient-4.5.12-javadoc.jar; 赠送源代码:httpclient-4.5.12-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.12.pom; 包含翻译后的API文档:...
赠送jar包:httpclient-4.5.10.jar; 赠送原API文档:httpclient-4.5.10-javadoc.jar; 赠送源代码:httpclient-4.5.10-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.10.pom; 包含翻译后的API文档:...
赠送jar包:httpclient-4.5.2.jar; 赠送原API文档:httpclient-4.5.2-javadoc.jar; 赠送源代码:httpclient-4.5.2-sources.jar; 包含翻译后的API文档:httpclient-4.5.2-javadoc-API文档-中文(简体)-英语-对照...
在实际开发中,我们需要以下步骤来使用HttpClient-4.0-alpha2: 1. 创建HttpClient实例:根据项目需求,可以设置连接池、超时时间、重试策略等。 ```java CloseableHttpClient httpClient = HttpClients.create...
赠送jar包:httpclient-4.2.5.jar; 赠送原API文档:httpclient-4.2.5-javadoc.jar; 赠送源代码:httpclient-4.2.5-sources.jar; 赠送Maven依赖信息文件:httpclient-4.2.5.pom; 包含翻译后的API文档:httpclient...
赠送jar包:httpclient-4.5.3.jar 赠送原API文档:httpclient-4.5.3-javadoc.jar 赠送源代码:httpclient-4.5.3-sources.jar 包含翻译后的API文档:httpclient-4.5.3-javadoc-API文档-中文(简体)版.zip 对应Maven...
赠送jar包:httpclient-4.4.jar; 赠送原API文档:httpclient-4.4-javadoc.jar; 赠送源代码:httpclient-4.4-sources.jar; 赠送Maven依赖信息文件:httpclient-4.4.pom; 包含翻译后的API文档:httpclient-4.4-...
赠送jar包:httpclient-4.5.2.jar; 赠送原API文档:httpclient-4.5.2-javadoc.jar; 赠送源代码:httpclient-4.5.2-sources.jar; 包含翻译后的API文档:httpclient-4.5.2-javadoc-API文档-中文(简体)版.zip ...
《HttpClient 4.0:Java中的HTTP客户端库详解》 HttpClient是Apache软件基金会下的一个开源项目,主要用于处理HTTP协议的客户端编程。HttpClient 4.0是其一个重要的版本,为开发者提供了强大的HTTP通信功能,广泛...
赠送jar包:httpclient-4.5.5.jar; 赠送原API文档:httpclient-4.5.5-javadoc.jar; 赠送源代码:httpclient-4.5.5-sources.jar; 包含翻译后的API文档:httpclient-4.5.5-javadoc-API文档-中文(简体)-英语-对照...