程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java讀取網站內容的兩種方法

java讀取網站內容的兩種方法

編輯:關於JAVA

 1,HttpClIEnt

  利用apache的虛擬客戶端包獲取某個地址的內容 1import Java.io.UnsupportedEncodingException;

  2import Java.util.HashSet;

  3import Java.util.Iterator;

  4import Java.util.Set;

  5import Java.util.regex.Matcher;

  6import Java.util.regex.Pattern;

  7

  8import org.apache.commons.httpclient.HttpClIEnt;

  9import org.apache.commons.httpclIEnt.NameValuePair;

  10import org.apache.commons.httpclIEnt.methods.PostMethod;

  11

  12public class catchMain {

  13

  14 /** *//**

  15 * @param args

  16 */

  17 public static void main(String[] args) {

  18

  19

  20 String url = "http://search.foodqs.com/companysearch.ASP";

  21 String keyWord="食";

  22 String response=createhttpClIEnt(url,keyWord);

  23 }

  24

  25public static String createhttpClIEnt(String url,String param){

  26 HttpClient client = new HttpClIEnt();

  27 String response=null;

  28 String keyWord=null;

  29 PostMethod postMethod = new PostMethod(url);

  30 try {

  31 if(param!=null)

  32 keyWord = new String(param.getBytes("gb2312"),"ISO-8859-1");

  33 } catch (UnsupportedEncodingException e1) {

  34 // TODO Auto-generated catch block

  35 e1.printStackTrace();

  36 }

  37

  38 NameValuePair[] data = { new NameValuePair("keyword", keyWord) };

  39 // 將表單的值放入postMethod中

  40 postMethod.setRequestBody(data);

  41

  42 try {

  43 int statusCode = clIEnt.executeMethod(postMethod);

  44 response = new String(postMethod.getResponseBodyAsString()

  45 .getBytes("ISO-8859-1"), "GBK");

  46 } catch (Exception e) {

  47

  48 e.printStackTrace();

  49 }

  50 return response;

  51

  52 }

  53

  2.Java自帶的HttpURLConnection

  1public static String getPageContent(String strUrl, String strPostRequest,

  2 int maxLength) {

  3 //讀取結果網頁

  4 StringBuffer buffer = new StringBuffer();

  5 System.setProperty("sun.Net.clIEnt.defaultConnectTimeout", "5000");

  6 System.setProperty("sun.Net.clIEnt.defaultReadTimeout", "5000");

  7 try {

  8 URL newUrl = new URL(strUrl);

  9 HttpURLConnection hConnect = (HttpURLConnection) newUrl

  10 .openConnection();

  11 //POST方式的額外數據

  12 if (strPostRequest.length() > 0) {

  13 hConnect.setDoOutput(true);

  14 OutputStreamWriter out = new OutputStreamWriter(hConnect

  15 .getOutputStream());

  16 out.write(strPostRequest);

  17 out.flush();

  18 out.close();

  19 }

  20 //讀取內容

  21 BufferedReader rd = new BufferedReader(new InputStreamReader(

  22 hConnect.getInputStream()));

  23 int ch;

  24 for (int length = 0; (ch = rd.read()) > -1

  25 && (maxLength <= 0 || length < maxLength); length++)

  26 buffer.append((char) ch);

  27 rd.close();

  28 hConnect.disconnect();

  29 return buffer.toString().trim();

  30 } catch (Exception e) {

  31 // return "錯誤:讀取網頁失敗!";

  32 return null;

  33 }

  34 }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved