<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "JSP精選實用範例(五):抓取網頁"]]></title>
		<link>https://forum.andowson.com/posts/list/5.page</link>
		<description><![CDATA[Latest messages posted in the topic "JSP精選實用範例(五):抓取網頁"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>JSP精選實用範例(五):抓取網頁</title>
				<description><![CDATA[ 預先安裝函式庫：[url=http://hc.apache.org/httpclient-3.x/index.html]Apache Commons HttpClient 3.x[/url], [url=http://commons.apache.org/codec/]Apache Commons Codec[/url], [url=http://commons.apache.org/logging/]Apache Commons Logging[/url] 
<br>
程式碼： 
<br>
httpclient.jsp: 
<br>
[code]&lt;%@ page contentType="text/html;charset=big5" %&gt; 
<br>
&lt;%@ page import="org.apache.commons.httpclient.*" %&gt; 
<br>
&lt;%@ page import="org.apache.commons.httpclient.methods.*" %&gt; 
<br>
&lt;%@ page import="org.apache.commons.httpclient.params.HttpMethodParams" %&gt; 
<br>
&lt;%@ page import="java.io.*" %&gt; 
<br>
&lt;% 
<br>
 String url = "http://tw.stock.yahoo.com/"; 
<br>
<br>
 String stockId = request.getParameter("stock_id"); 
<br>
 if (stockId != null) { 
<br>
 url += "q/q?s="+stockId; 
<br>
 } 
<br>
<br>
 // Create an instance of HttpClient. 
<br>
 HttpClient client = new HttpClient(); 
<br>
<br>
 // Create a method instance. 
<br>
 HttpMethod method = new GetMethod(url); 
<br>
<br>
 // Provide custom retry handler is necessary 
<br>
 method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
<br>
 new DefaultHttpMethodRetryHandler(3, false)); 
<br>
<br>
 try { 
<br>
 // Execute the method. 
<br>
 int statusCode = client.executeMethod(method); 
<br>
<br>
 if (statusCode != HttpStatus.SC_OK) { 
<br>
 System.err.println("Method failed: " + method.getStatusLine()); 
<br>
 } 
<br>
<br>
 // Read the response body. 
<br>
 byte[] responseBody = method.getResponseBody(); 
<br>
<br>
 // Deal with the response. 
<br>
 // Use caution: ensure correct character encoding and is not binary data 
<br>
 //System.out.println(new String(responseBody)); 
<br>
<br>
 String result = new String(responseBody, "Big5"); 
<br>
 if (stockId != null) { 
<br>
 result = result.substring(result.indexOf("nowrap&gt;&lt;b&gt;")+"nowrap&gt;&lt;b&gt;".length()); 
<br>
 result = result.substring(0, result.indexOf("&lt;/b&gt;")); 
<br>
 out.println(stockId + " Price Now: " + result); 
<br>
 } else { 
<br>
 out.println(result); 
<br>
 } 
<br>
 } catch (HttpException e) { 
<br>
 System.err.println("Fatal protocol violation: " + e.getMessage()); 
<br>
 e.printStackTrace(); 
<br>
 } catch (IOException e) { 
<br>
 System.err.println("Fatal transport error: " + e.getMessage()); 
<br>
 e.printStackTrace(); 
<br>
 } finally { 
<br>
 // Release the connection. 
<br>
 method.releaseConnection(); 
<br>
 } 
<br>
%&gt;[/code] 
<br>
參考資料： 
<br>
http://hc.apache.org/httpclient-3.x/tutorial.html]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/215/326.page</guid>
				<link>https://forum.andowson.com/posts/preList/215/326.page</link>
				<pubDate><![CDATA[Mon, 28 Jan 2008 23:25:36]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
			<item>
				<title>回覆:JSP精選實用範例(五):抓取網頁</title>
				<description><![CDATA[ 改用[url=http://hc.apache.org/httpcomponents-client-ga/]Apache HttpComponents HttpClient 4[/url]撰寫的版本： 
<br>
httpclient4.jsp: 
<br>
[code=java]&lt;%@ page contentType="text/html;charset=big5" %&gt; 
<br>
&lt;%@ page import="java.io.IOException" %&gt; 
<br>
&lt;%@ page import="org.apache.http.client.ClientProtocolException" %&gt; 
<br>
&lt;%@ page import="org.apache.http.client.HttpClient" %&gt; 
<br>
&lt;%@ page import="org.apache.http.client.ResponseHandler" %&gt; 
<br>
&lt;%@ page import="org.apache.http.client.methods.HttpGet" %&gt; 
<br>
&lt;%@ page import="org.apache.http.impl.client.BasicResponseHandler" %&gt; 
<br>
&lt;%@ page import="org.apache.http.impl.client.DefaultHttpClient" %&gt; 
<br>
&lt;% 
<br>
 String url = "http://tw.stock.yahoo.com/"; 
<br>
<br>
 String stockId = request.getParameter("stock_id"); 
<br>
 if (stockId != null) { 
<br>
 url += "q/q?s="+stockId; 
<br>
 } 
<br>
<br>
 // Create an instance of HttpClient. 
<br>
 HttpClient httpclient = new DefaultHttpClient(); 
<br>
 try { 
<br>
 // Create an HttpGet method instance. 
<br>
 HttpGet httpget = new HttpGet(url); 
<br>
<br>
 System.out.println("executing request " + httpget.getURI()); 
<br>
<br>
 // Create a response handler 
<br>
 ResponseHandler&lt;String&gt; responseHandler = new BasicResponseHandler(); 
<br>
 String responseBody = httpclient.execute(httpget, responseHandler); 
<br>
 if (stockId != null) { 
<br>
 responseBody = responseBody.substring(responseBody.indexOf("nowrap&gt;&lt;b&gt;")+"nowrap&gt;&lt;b&gt;".length()); 
<br>
 responseBody = responseBody.substring(0, responseBody.indexOf("&lt;/b&gt;")); 
<br>
 out.println(stockId + " Price Now: " + responseBody); 
<br>
 } else { 
<br>
 out.println(responseBody); 
<br>
 } 
<br>
 } catch (ClientProtocolException e) { 
<br>
 e.printStackTrace(); 
<br>
 } catch (IOException e) { 
<br>
 e.printStackTrace(); 
<br>
 } finally { 
<br>
 // When HttpClient instance is no longer needed, 
<br>
 // shut down the connection manager to ensure 
<br>
 // immediate deallocation of all system resources 
<br>
 httpclient.getConnectionManager().shutdown(); 
<br>
 } 
<br>
%&gt; 
<br>
[/code]]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/215/984.page</guid>
				<link>https://forum.andowson.com/posts/preList/215/984.page</link>
				<pubDate><![CDATA[Tue, 29 Mar 2011 00:15:32]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
			<item>
				<title>回覆:JSP精選實用範例(五):抓取網頁</title>
				<description><![CDATA[ 如果是在公司內部環境使用HttpClient 4去連外部網站須走proxy才能通時， 
<br>
將原來這行: 
<br>
[code]HttpClient httpclient = new DefaultHttpClient();[/code] 
<br>
修改為(假設proxy是http://10.160.3.88:8080/): 
<br>
[code]HttpHost proxy = new HttpHost("10.160.3.88", 8080); 
<br>
HttpClient httpclient = new DefaultHttpClient(); 
<br>
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);[/code] 
<br>
同時補上宣告: 
<br>
[code]&lt;% @page import="org.apache.http.HttpHost" %&gt; 
<br>
&lt;% @page import="org.apache.http.conn.params.ConnRoutePNames" %&gt;[/code] 
<br>
即可。]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/215/1226.page</guid>
				<link>https://forum.andowson.com/posts/preList/215/1226.page</link>
				<pubDate><![CDATA[Fri, 11 May 2012 17:52:17]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
	</channel>
</rss>