java ץȡÍøÒ³ÄÚÈÝ,¿ÉÉ趨´úÀí(HttpURLConnection)
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.Properties; /** **Íøҳץȡ ͨÓÃÀà * @author */ public class WebClient { /** *´úÀí·þÎñÆ÷µÄµØÖ· */ private static String proxyHost; /** * ´úÀí·þÎñÆ÷µÄ¶Ë¿Ú */ private static String proxyPort; /** * ´úÀí·þÎñÆ÷Óû§Ãû */ private static String proxyUser; /** * ´úÀí·þÎñÆ÷ÃÜÂë */ private static String proxyPassword; /** *Íøҳץȡ·½·¨ * @param urlString ҪץȡµÄurlµØÖ· * @param charset ÍøÒ³±àÂ뷽ʽ * @param timeout ³¬Ê±Ê±¼ä * @return ץȡµÄÍøÒ³ÄÚÈÝ * @throws IOException ץȡÒì³£ */ public static String GetWebContent(String urlString, final String charset, int timeout) throws IOException { if (urlString == null || urlString.length() == 0) { return null; } urlString = (urlString.startsWith("http://") || urlString.startsWith("https://")) ? urlString : ("http://" + urlString).intern(); URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); GetProxy(); conn.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");//Ôö¼Ó±¨Í·£¬Ä£Äâä¯ÀÀÆ÷£¬·ÀÖ¹ÆÁ±Î conn.setRequestProperty("Accept", "text/html");//Ö»½ÓÊÜtext/htmlÀàÐÍ£¬µ±È»Ò²¿ÉÒÔ½ÓÊÜͼƬ,pdf,*/*ÈÎÒ⣬¾ÍÊÇtomcat/conf/webÀïÃ涨ÒåÄÇЩ conn.setConnectTimeout(timeout); try { if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) { return null; } } catch (IOException e) { e.printStackTrace(); return null; } InputStream input = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(input, charset)); String line = null; StringBuffer sb = new StringBuffer(); while ((line = reader.readLine()) != null) { sb.append(line).append("\r\n"); } if (reader != null) { reader.close(); } if (conn != null) { conn.disconnect(); } return sb.toString(); } /** * Íøҳץȡ·½·¨ * @param urlString ҪץȡµÄurlµØÖ· * @return ץȡµÄÍøÒ³ÄÚÈÝ * @throws IOException ץȡÒì³£ */ public static String GetWebContent(String urlString) throws IOException { return GetWebContent(urlString, "iso-8859-1", 5000); } /** * Íøҳץȡ·½·¨ * @param urlString ҪץȡµÄurlµØÖ· * @param pageCharset Ä¿±êÍøÒ³±àÂ뷽ʽ * @return ץȡµÄÍøÒ³ÄÚÈÝ * @throws IOException ץȡÒì³£ */ public static String GetWebContent(String urlString, String pageCharset) throws IOException { String strHTML = GetWebContent(urlString, "iso-8859-1", 5000); String StrEncode = new String(strHTML.getBytes("iso-8859-1"), pageCharset); return StrEncode; } /** * É趨´úÀí·þÎñÆ÷ * @param proxyHost * @param proxyPort */ public static void SetProxy(String proxyHost, String proxyPort) { SetProxy(proxyHost, proxyPort, null, null); } /** * É趨´úÀí·þÎñÆ÷ * @param proxyHost ´úÀí·þÎñÆ÷µÄµØÖ· * @param proxyPort ´úÀí·þÎñÆ÷µÄ¶Ë¿Ú * @param proxyUser ´úÀí·þÎñÆ÷Óû§Ãû * @param proxyPassword ´úÀí·þÎñÆ÷ÃÜÂë */ public static void SetProxy(String sproxyHost, String sproxyPort, String sproxyUser, String sproxyPassword) { proxyHost = sproxyHost; proxyPort = sproxyPort; if (sproxyPassword != null && sproxyPassword.length() > 0) { proxyUser = sproxyUser; proxyPassword = sproxyPassword; } } /** * È¡µÃ´úÀíÉ趨 * @return */ private static Properties GetProxy() { Properties propRet = null; if (proxyHost != null && proxyHost.length() > 0) { propRet = System.getProperties(); // ÉèÖÃhttp·ÃÎÊҪʹÓõĴúÀí·þÎñÆ÷µÄµØÖ· propRet.setProperty("http.proxyHost", proxyHost); // ÉèÖÃhttp·ÃÎÊҪʹÓõĴúÀí·þÎñÆ÷µÄ¶Ë¿Ú propRet.setProperty("http.proxyPort", proxyPort); if (proxyUser != null && proxyUser.length() > 0) { //Óû§ÃûÃÜÂë propRet.setProperty("http.proxyUser", proxyUser); propRet.setProperty("http.proxyPassword", proxyPassword); } } return propRet; } /** * Àà²âÊÔº¯Êý * @param args * @throws IOException */ public static void main(String[] args) throws IOException { //SetProxy("10.10.10.10", "8080");//´úÀí·þÎñÆ÷É趨 String s = GetWebContent("http://www.my400800.cn", "utf-8"); System.out.println(s); } }
ÍƼöÐÅÏ¢
- ÔÝÎ޼Ǽ
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- java urlÖØд¼¼Êõ(UrlReWriter)
- [ת]JavaµÄÊý×飨Array£©¡¢Vector¡¢ArrayLis....
- jsp»·¾³ÏÂajaxÂÒÂëÎÊÌâµÄ½â¾ö
- JSP¼¼Êõ Ö¸Áî ÂÒÂëÎÊÌâ ÔËÐÐÔÀíºÍ¾Å´óÒþʽ¶ÔÏó web¿ª·¢µÄË....
- JAVA ˮӡ ͼƬ ÎÄ×Ö
- java¶þ½øÖÆ£¬ÔÂë¡¢·´Âë¡¢²¹Â롢λÔËËã
- Java WebÓ¦ÓÃÆô¶¯¼ä¸ôÖ´ÐеijÌÐò
- jspÉú³ÉxmlÎļþʾÀý
- javaÖÐÀà±äÁ¿¡¢Àà·½·¨¡¢ÊµÀý±äÁ¿¡¢ÊµÀý·½·¨
- JSPÒ³ÃæµÄÎåÖÖÌøת·½·¨
ÆÀÂÛ