練功房推薦書單

  • 猛虎出柙雙劍合璧版--最新 OCA / OCP Java SE 7 Programmer 專業認證 (電子書)
  • 流浪教師存零股存到3000萬(全新增修版)(書+DVD)
  • 開始在關西自助旅行(京都‧大阪‧神戶‧奈良)(全新增訂版)
  • 不敗教主的300張股票存股術
JSP精選實用範例(五):抓取網頁 RSS feed
Forum Index » 網頁程式設計 Web Development
Author Message
andowson

七段學員
[Avatar]

Joined: 2007/1/2
Messages: 706
Location: 台北
Offline
預先安裝函式庫:Apache Commons HttpClient 3.x, Apache Commons Codec, Apache Commons Logging
程式碼:
httpclient.jsp:
<%@ page contentType="text/html;charset=big5" %>
<%@ page import="org.apache.commons.httpclient.*" %>
<%@ page import="org.apache.commons.httpclient.methods.*" %>
<%@ page import="org.apache.commons.httpclient.params.HttpMethodParams" %>
<%@ page import="java.io.*" %>
<%    
    String url = "http://tw.stock.yahoo.com/";
    
    String stockId = request.getParameter("stock_id");
    if (stockId != null) { 
      url += "q/q?s="+stockId;
    }
    
    // Create an instance of HttpClient.
    HttpClient client = new HttpClient();

    // Create a method instance.
    HttpMethod method = new GetMethod(url);
    
    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
    		new DefaultHttpMethodRetryHandler(3, false));

    try {
      // Execute the method.
      int statusCode = client.executeMethod(method);

      if (statusCode != HttpStatus.SC_OK) {
        System.err.println("Method failed: " + method.getStatusLine());
      }

      // Read the response body.
      byte[] responseBody = method.getResponseBody();

      // Deal with the response.
      // Use caution: ensure correct character encoding and is not binary data
      //System.out.println(new String(responseBody));
      
      String result = new String(responseBody, "Big5");
      if (stockId != null) {        
        result = result.substring(result.indexOf("nowrap><b>")+"nowrap><b>".length());
        result = result.substring(0, result.indexOf("</b>"));
        out.println(stockId + " Price Now: " + result);
      } else {
        out.println(result);
      }
    } catch (HttpException e) {
      System.err.println("Fatal protocol violation: " + e.getMessage());
      e.printStackTrace();
    } catch (IOException e) {
      System.err.println("Fatal transport error: " + e.getMessage());
      e.printStackTrace();
    } finally {
      // Release the connection.
      method.releaseConnection();
    } 
%>

參考資料:
http://hc.apache.org/httpclient-3.x/tutorial.html
 Filename httpclient.jsp [Disk] Download
 Description HttpClient抓網頁範例程式
 Filesize 2 Kbytes
 Downloaded:  89 time(s)


分享經驗 累積智慧
[WWW] [MSN]
andowson

七段學員
[Avatar]

Joined: 2007/1/2
Messages: 706
Location: 台北
Offline
改用Apache HttpComponents HttpClient 4撰寫的版本:
httpclient4.jsp:
<%@ page contentType="text/html;charset=big5" %>
<%@ page import="java.io.IOException" %>
<%@ page import="org.apache.http.client.ClientProtocolException" %>
<%@ page import="org.apache.http.client.HttpClient" %>
<%@ page import="org.apache.http.client.ResponseHandler" %>
<%@ page import="org.apache.http.client.methods.HttpGet" %>
<%@ page import="org.apache.http.impl.client.BasicResponseHandler" %>
<%@ page import="org.apache.http.impl.client.DefaultHttpClient" %>
<%    
    String url = "http://tw.stock.yahoo.com/";
    
    String stockId = request.getParameter("stock_id");
    if (stockId != null) { 
      url += "q/q?s="+stockId;
    }
    
   // Create an instance of HttpClient.
    HttpClient httpclient = new DefaultHttpClient();
    try {
    	// Create an HttpGet method instance.
    	HttpGet httpget = new HttpGet(url);

        System.out.println("executing request " + httpget.getURI());

        // Create a response handler
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httpget, responseHandler);
        if (stockId != null) {
        	responseBody = responseBody.substring(responseBody.indexOf("nowrap><b>")+"nowrap><b>".length());
        	responseBody = responseBody.substring(0, responseBody.indexOf("</b>"));					
    		out.println(stockId + " Price Now: " + responseBody);
        } else {
        	out.println(responseBody);
        }
    } catch (ClientProtocolException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }   
%>
 Filename httpclient4.jsp [Disk] Download
 Description HttpClient 4抓網頁範例程式
 Filesize 2 Kbytes
 Downloaded:  25 time(s)


分享經驗 累積智慧
[WWW] [MSN]
andowson

七段學員
[Avatar]

Joined: 2007/1/2
Messages: 706
Location: 台北
Offline
如果是在公司內部環境使用HttpClient 4去連外部網站須走proxy才能通時,
將原來這行:
HttpClient httpclient = new DefaultHttpClient();

修改為(假設proxy是http://10.160.3.88:8080/):
HttpHost proxy = new HttpHost("10.160.3.88", 8080);
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

同時補上宣告:
<% @page import="org.apache.http.HttpHost" %>
<% @page import="org.apache.http.conn.params.ConnRoutePNames" %>

即可。

分享經驗 累積智慧
[WWW] [MSN]
 
Forum Index » 網頁程式設計 Web Development
Go to:   
Mobile view