- 浏览: 468343 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (146)
- Maven (3)
- Quartz (10)
- Hessian (3)
- JDK (42)
- eclipse (4)
- 设计思想 (6)
- XML (8)
- JavaMail (1)
- Spring (11)
- mina (1)
- HsqlDb (1)
- Cache (2)
- Tool (6)
- 心情 (5)
- JQuery (0)
- Hadoop (5)
- Hbase (3)
- 自动构建 (7)
- JNDI (0)
- 代码赏析 (5)
- Oracle (1)
- Excel (4)
- Effective Java (5)
- JAXB (4)
- fdafasdf (1)
- ccc (0)
- web (3)
- concurrent (1)
- CVS (1)
- eclipse plugin (2)
- Apache (10)
最新评论
-
chxiaowu:
nice!
Quartz实现固定执行次数 -
zxjlwt:
学习了。http://surenpi.com
自定义ClassLoader -
kadlly:
public static final Logger log ...
Hessian 权限认证 -
spring_springmvc:
java程序语言学习教程 地址http://www.zuida ...
Java-Final -
liushuiwuyan:
[img][/img]
设计模式-单例
使用HttpClient来发送请求获取数据最经典,以下呢我们使用jdk自带的HttpUrlConnection来操作, 很简单,发个请求取出数据,还可以条过https的验证.
package org.ycl.commons.text; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.MalformedURLException; import java.net.Proxy; import java.net.URL; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.commons.io.LineIterator; /** * Functions: * * 1. getInputStream(String url)/getInputStream(String url, Proxy proxy) * <li>- get InputStream from url with proxy(or not)</li> * 2. getString(String url) * <li>- get String from url with one line</li> * <li>- this is simple get html content, {@link HttpClient}</li> * 3. getStringList(String url) * <li>- get List<String> from url with any lines</li> * 4. getStringToday(String urlstring) * <li>- get String from url and save copy in file.</li> * 5. writeFileToday(String urlstring) * <li>- wirte urlstring content to file</li> * 6. needWriteFileToday(String urlstring) * <li>- check this file is generator today, or will be re-write file</li> * 7. getURLFile(String urlstring) * <li>- via urlstring to generator file</li> * 8. writeFile(String url, File file) * <li>- write url content to file</li> * 9. htmlEscape(String input)/htmlUnescape(String input) * <li>- turn Html language to transferred meaning, or reverse.</li> * * * NOTE:this is from my tool box * * {@link org.springframework.web.util.HtmlUtils} * @author e557400 * */ public class HtmlUtils { public static String DEFAULT_CONNECTION_POST = "POST";// request in "POST" method public static String DEFAULT_CONNECTION_GET = "GET";// request in "POST" method public static boolean DEFAULT_CONNECTION_DOOUTPUT = false;// if you intend to use the URL connection for output public static boolean DEFAULT_CONNECTION_ALLOWUSERINTERACTION = false;// Don't need to interaction with user, exp:Applet public static boolean DEFAULT_CONNECTION_DOINPUT = true;// if you intend to use the URL connection for input public static boolean DEFAULT_CONNECTION_FOLLOWREDIRECTS = true;//default is true public static boolean DEFAULT_DEBUG = true;// if DEBUG is true, will be print error message public static boolean DEFAULT_SKIP_SSL = false;// if we vistor https, should be skip ssl validate? public static String DEFAULT_ENCODE = "UTF-8";// we read html use this encode. public static String DEFAULT_HTML_FOLDER = "/usr";// we read html use this encode. public static int DEFAULT_CONNECTION_CONN_TIMEOUT = 3;// timeout in minutes public static int DEFAULT_CONNECTION_READ_TIMEOUT = 3;// timeout in minutes public static Proxy DEFAULT_PROXY = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.statestr.com", 80)); public static boolean DEFAULT_PROXY_FLAG = false; /** * The number of second is 1000 milliseconds. */ public static final int ONE_SEC = 1000; /** * The number of minute is 60 second */ public static final int ONE_MIN = ONE_SEC * 60; // remove in product env. static { DEFAULT_PROXY_FLAG = true; } /** * override default proxy * * @param proxy */ public static void setDefaultProxy(Proxy proxy) { DEFAULT_PROXY = proxy; } /** * main set Connection attribute of * requestMethod,ConnectTimeout,ReadTimeout. * * @param urlstring * @return * @throws IOException */ private static HttpURLConnection initConnection(String urlstring) throws IOException { return initConnection(urlstring, null); } /** * main set Connection attribute of * requestMethod,ConnectTimeout,ReadTimeout. we can give Proxy, or use * default Proxy, or no Proxy. * * @param urlstring * @param proxy * @return * @throws IOException */ private static HttpURLConnection initConnection(String urlstring, Proxy proxy) throws IOException { URL url = new URL(urlstring); HttpURLConnection conn = null; if (proxy != null) { conn = (HttpURLConnection) url.openConnection(proxy); } else { if (DEFAULT_PROXY_FLAG) { conn = (HttpURLConnection) url.openConnection(DEFAULT_PROXY); } else { conn = (HttpURLConnection) url.openConnection(); } } //NOTE: SSL valid must be set first, or will be unusable. if(DEFAULT_SKIP_SSL){ try{ // Create a trust manager that does not validate certificate chains final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { @Override public void checkClientTrusted( final X509Certificate[] chain, final String authType ) { } @Override public void checkServerTrusted( final X509Certificate[] chain, final String authType ) { } @Override public X509Certificate[] getAcceptedIssuers() { return null; } } }; // Install the all-trusting trust manager final SSLContext sslContext = SSLContext.getInstance( "SSL" ); sslContext.init( null, trustAllCerts, new java.security.SecureRandom() ); // Create an ssl socket factory with our all-trusting manager final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); ( (HttpsURLConnection) conn ).setSSLSocketFactory(sslSocketFactory); ( (HttpsURLConnection) conn ).setHostnameVerifier(new HostnameVerifier(){ @Override public boolean verify(String arg0, SSLSession arg1) { // TODO Auto-generated method stub return true; } }); }catch(Exception e){ if (DEFAULT_DEBUG) { e.printStackTrace(); } } } conn.setRequestMethod(DEFAULT_CONNECTION_GET); conn.setConnectTimeout(DEFAULT_CONNECTION_CONN_TIMEOUT * ONE_MIN); conn.setReadTimeout(DEFAULT_CONNECTION_READ_TIMEOUT * ONE_MIN); // set request property // conn.setRequestProperty("Content-Type", // "application/x-www-form-urlencoded"); // conn.setRequestProperty("Content-Type", "text/html; charset=utf-8"); // conn.setRequestProperty("Accept-Language", "en-US"); // conn.setRequestProperty("Accept", // "text/html, application/xhtml+xml, */*"); // conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); // conn.setRequestProperty("User-Agent", // "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"); // conn.setRequestProperty("Content-Length","10"); conn.setAllowUserInteraction(DEFAULT_CONNECTION_ALLOWUSERINTERACTION); conn.setDoOutput(DEFAULT_CONNECTION_DOOUTPUT); conn.setDoInput(DEFAULT_CONNECTION_DOINPUT); if (DEFAULT_DEBUG) { Map<String, List<String>> headers = conn.getHeaderFields(); if (headers != null) { System.out.println("begin header"); for (Map.Entry<String, List<String>> header : headers .entrySet()) { System.out.println("key:" + header.getKey()); System.out.println("value:" + header.getValue()); } System.out.println("end header"); } } return conn; } /** * Unconditionally close a <code>Closeable</code>. * <p> * Equivalent to {@link Closeable#close()}, except any exceptions will be * ignored. This is typically used in finally blocks. * <p> * Example code: * * <pre> * Closeable closeable = null; * try { * closeable = new FileReader("foo.txt"); * // process closeable * closeable.close(); * } catch (Exception e) { * // error handling * } finally { * IOUtils.closeQuietly(closeable); * } * </pre> * * @param closeable * the object to close, may be null or already closed * @since 2.0 */ private static void closeQuietly(Closeable closeable) { try { if (closeable != null) { closeable.close(); } } catch (IOException ioe) { // ignore } } /** * @{link org.apache.commons.io.IOUtils} Return an Iterator for the lines in * a <code>Reader</code>. * <p> * <code>LineIterator</code> holds a reference to the open * <code>Reader</code> specified here. When you have finished with * the iterator you should close the reader to free internal * resources. This can be done by closing the reader directly, or by * calling {@link LineIterator#close()} or * {@link LineIterator#closeQuietly(LineIterator)}. * <p> * The recommended usage pattern is: * * <pre> * try { * LineIterator it = IOUtils.lineIterator(reader); * while (it.hasNext()) { * String line = it.nextLine(); * // / do something with line * } * } finally { * IOUtils.closeQuietly(reader); * } * </pre> * * @param reader * the <code>Reader</code> to read from, not null * @return an Iterator of the lines in the reader, never null * @throws IllegalArgumentException * if the reader is null * @since 1.2 */ private static void closeQuietly(Reader input) { closeQuietly((Closeable) input); } /** * get URL content with InputStream * * @param url * @return * @throws IOException */ public static InputStream getInputStream(String url) throws IOException { HttpURLConnection conn = initConnection(url); InputStream is = null; if (conn.getResponseCode() >= 400) { is = conn.getErrorStream(); } else { is = conn.getInputStream(); } return is; } /** * get URL content with InputStream with Proxy * * @param url * @param proxy * @return * @throws IOException */ public static InputStream getInputStream(String url, Proxy proxy) throws IOException { HttpURLConnection conn = initConnection(url, proxy); InputStream is = conn.getInputStream(); return is; } /** * get URL content with String. 1. success return content. 2. fail null * * @param url * @return */ public static String getString(String url) { BufferedReader in = null; StringBuffer sb = new StringBuffer(); try { in = new BufferedReader(new InputStreamReader(getInputStream(url), DEFAULT_ENCODE)); String inputLine; while ((inputLine = in.readLine()) != null) { sb.append(inputLine); } } catch (IOException e) { if (DEFAULT_DEBUG) { e.printStackTrace(); } return null; } finally { closeQuietly(in); } return sb.toString(); } public static List<String> getStringList(String url) { BufferedReader in = null; List<String> contents = new ArrayList<String>(); try { in = new BufferedReader(new InputStreamReader(getInputStream(url), DEFAULT_ENCODE)); String inputLine; while ((inputLine = in.readLine()) != null) { contents.add(inputLine); } } catch (IOException e) { if (DEFAULT_DEBUG) { e.printStackTrace(); } return null; } finally { closeQuietly(in); } return contents; } /** * get url to string, if this file is exist, then read it, or read from URL. * * @param urlstring * @return */ public static String getStringToday(String urlstring) { BufferedReader in = null; try { writeFileToday(urlstring); StringBuffer sb = new StringBuffer(); File file = getURLFile(urlstring); in = new BufferedReader(new InputStreamReader( FileUtils.openInputStream(file), DEFAULT_ENCODE)); String inputLine; while ((inputLine = in.readLine()) != null) { sb.append(inputLine); sb.append(System.getProperty("line.separator")); } // Scanner scanner = new Scanner(new FileInputStream(file), // DEFAULT_ENCODE); // while (scanner.hasNextLine()){ // sb.append(scanner.nextLine()); // } // scanner.close(); return sb.toString(); } catch (IOException e) { if (DEFAULT_DEBUG) { e.printStackTrace(); } return null; } finally { closeQuietly(in); } } /** * we may be read URL content to file, if we have read, so next test we just * get from file. not EveryTime from URL, it can save so may times. * * @param url * @param fileName */ public static void writeFile(String url, File file) { BufferedReader in = null; BufferedWriter fw = null; try { in = new BufferedReader(new InputStreamReader(getInputStream(url), DEFAULT_ENCODE)); fw = new BufferedWriter(new OutputStreamWriter( FileUtils.openOutputStream(file), DEFAULT_ENCODE)); String inputLine; while ((inputLine = in.readLine()) != null) { fw.write(inputLine); fw.write(System.getProperty("line.separator")); } } catch (IOException e) { if (DEFAULT_DEBUG) { e.printStackTrace(); } } finally { closeQuietly(in); closeQuietly(fw); } } /** * add file in weather folder * * how to judge we have download today, every day file is difference * * @param url */ public static void writeFileToday(String urlstring) { if (needWriteFileToday(urlstring)) { writeFile(urlstring, getURLFile(urlstring)); } } /** * Path /weather/101210101.shtml so we will be generator file in this * derectory. * * @param urlstring */ public static boolean needWriteFileToday(String urlstring) { File file = getURLFile(urlstring); Long lastmodify = file.lastModified(); Long todaymodify = new Date().getTime(); if (todaymodify - lastmodify < DateUtils.MILLIS_PER_DAY) { return false; } return true; } /** * via url generator file * * @param urlstring * @return */ public static File getURLFile(String urlstring) { URL url = null; File file = null; try { url = new URL(urlstring); String path = url.getPath(); file = new File(DEFAULT_HTML_FOLDER + path+ DateUtils.getDateAsString(new Date(), "MM-dd-yyyy") .concat(".html")); } catch (MalformedURLException e) { if (DEFAULT_DEBUG) { e.printStackTrace(); } } return file; } /** * Turn special characters into HTML character references. * Handles complete character set defined in HTML 4.01 recommendation. * <p>Escapes all special characters to their corresponding * entity reference (e.g. {@code <}). * <p>Reference: * <a href="http://www.w3.org/TR/html4/sgml/entities.html"> * http://www.w3.org/TR/html4/sgml/entities.html * </a> * @param input the (unescaped) input string * @return the escaped string */ public static String htmlEscape(String input) { return org.springframework.web.util.HtmlUtils.htmlEscape(input); } /** * Turn HTML character references into their plain text UNICODE equivalent. * <p>Handles complete character set defined in HTML 4.01 recommendation * and all reference types (decimal, hex, and entity). * <p>Correctly converts the following formats: * <blockquote> * &#<i>Entity</i>; - <i>(Example: &amp;) case sensitive</i> * &#<i>Decimal</i>; - <i>(Example: &#68;)</i><br> * &#x<i>Hex</i>; - <i>(Example: &#xE5;) case insensitive</i><br> * </blockquote> * Gracefully handles malformed character references by copying original * characters as is when encountered.<p> * <p>Reference: * <a href="http://www.w3.org/TR/html4/sgml/entities.html"> * http://www.w3.org/TR/html4/sgml/entities.html * </a> * @param input the (escaped) input string * @return the unescaped string */ public static String htmlUnescape(String input) { return org.springframework.web.util.HtmlUtils.htmlUnescape(input); } public static void main(String args[]) throws Exception { //List<String>contexts=getStringList("http://www.weather.com.cn/weather/101210101.shtml");//101210101 //String context =getStringToday("http://weather.yahooapis.com/forecastrss?w=1940345"); //String context = getStringToday("http://weather.yahooapis.com/forecastrss?w=2502265"); String context = getStringToday("http://weather.yahooapis.com/forecastrss?p=CHXX0044&u=c"); //String context = getStringToday("http://m.weather.com.cn/data/101270803.html"); //String context = getStringToday("http://m.weather.com.cn/data/101210101.html"); //String context = getStringToday("http://www.google.com"); //String context = getStringToday("http://www.baidu.com"); //String context = getStringToday("http://www.weather.com.cn/weather/101210101.shtml"); //String context = getStringToday("https://aplmd5.it.statestr.com:9445/PALMSServiceWEB/cacheReset"); //String context = getStringToday("http://aplmd5.it.statestr.com:9080/PLM/login.do"); //String context = getStringToday("http://aplmd5.it.statestr.com:9080/PLM/unittest/testproperties.jsp"); // for(String context:contexts) String escape = htmlEscape(context); System.out.println(escape); System.out.println(htmlUnescape(escape)); } }
发表评论
-
commons-httpClient Helper
2016-09-27 19:34 798使用HttpClient来发送请求获取数据最经典,以下呢我们使 ... -
commons-logging
2015-04-13 10:22 948我想,这个包是Apache开源里面用的最多的包,被各种开源使用 ... -
commons-io FileUtils Helper
2015-04-13 10:10 1913commons-io主要对输入流,输出流的打开和关闭,主要是对 ... -
commons-lang
2015-04-13 10:04 695这个包处了String和Number的封装,还有对Date, ... -
commons-lang NumberUtilHelper
2015-04-13 09:50 1294除了对字符串的复杂处理,对于数字,我们也是头大啊,类型,位移等 ... -
commons-lang StringUtilHelper
2015-04-13 09:40 975其实对字符串的处理,一般一个项目的core会建一个,最经典的算 ... -
commons-dbutils Helper VS JDBCTemplate
2015-04-10 17:03 3823这两个JDBC轻量分装框架的确都是刚刚的。 但是相对来说co ... -
commons-dbutils Helper
2015-04-09 17:00 3178封装下dbutils, 使用这个help去操作数据库会非常的方 ... -
commons-dbutils
2015-04-09 11:26 1490现在ORM框架很多,什么Hibernate,ibatis等等。 ...
相关推荐
包括了httpclient的所有包,commons-httpclient3.0.jar,httpclient4.0.jar,commons-logging1.1.1.jar,commons-codec-1.3.jar等
然而,需要注意的是,`commons-httpclient-3.1.jar`已经是较旧的版本,现代项目可能倾向于使用更新的Apache HttpClient库(如HttpClient 4.x或HttpComponents Client)或其它现代HTTP客户端,如OkHttp。
《JAVA中使用HttpClient:commons-httpclient-3.0.jar详解》 在JAVA开发中,进行HTTP请求时,Apache的HttpClient库是一个不可或缺的工具。本文将深入解析`commons-httpclient-3.0.jar`,它是HttpClient的一个重要...
http://jakarta.apache.org/commons/httpclient/ org.apache.commons.httpclient.URI org.apache.commons.httpclient.Wire org.apache.commons.httpclient.Cookie org.apache.commons.httpclient.Header org.apache.commons...
commons-httpclient
由于JDK内置的java.net.URL和URLConnection类在功能上可能不足以满足复杂的需求,Commons-HTTPClient 提供了更为丰富和灵活的功能。 HttpClient 支持HTTP 1.0和1.1协议的全部方法,包括GET、POST、PUT、DELETE、...
标题中的"commons-httpclient3.1.jar,commons-codec1.3.jar,commons-logging1.1.1.jar"指的是三个关键的Java库文件,它们是Apache HttpClient项目的一部分,用于在Java应用程序中实现HTTP通信。这些JAR(Java ...
commons-httpclient-3.0.jar JAVA中使用HttpClient可以用到
在生成Web服务客户端时,XFire可能会依赖于如`commons-codec-1.3.jar`和`commons-httpclient-3.0.jar`这样的第三方库。`commons-codec`可以帮助XFire处理编码解码问题,而`commons-httpclient`则提供了与远程Web服务...
在现代的Java开发中,虽然`commons-httpclient`已经被`HttpURLConnection`和`Apache HttpClient 4.x`(一个更新的分支)所取代,但因为其简单易用的API,`commons-httpclient`仍然在很多旧项目中被广泛使用。...
本场景中涉及的三个关键库是“commons-logging-1.1.1.jar”,“commons-httpclient-3.1.jar”以及“commons-codec-1.4.jar”。这三个jar包分别提供了日志记录、HTTP客户端通信和编码解码功能,是实现短信发送不可或...
5个jar包,commons-codec-1.9.jar,commons-httpclient-3.1.jar,commons-logging-1.2.jar,httpclient-4.5.jar,httpcore-4.4.1.jar
在项目中集成HttpClient,通常需要将`commons-httpclient-3.1-rc1.jar`添加到项目的类路径中,然后通过创建HttpClient实例,设置必要的配置,并调用相关方法发起HTTP请求。 接下来是`commons-logging.jar`,它是...
标题中的"commons-httpclient.jar,commons-codec.jar,commons-logging.jar"是Java开发中常用的三个库文件,它们各自服务于不同的功能领域。 首先,`commons-httpclient.jar`是Apache Commons HttpClient库,它是...
commons-httpclient-2.0.jar
commons-httpclient-3.1.1.jar
2. **commons-httpclient-3.0.1.jar**: 这就是Apache Commons HttpClient的核心库。这个版本3.0.1是该库的一个稳定版本,它包含了处理HTTP请求和响应的类和方法。HttpClient提供了异步和同步两种模式来发送GET、POST...