轉(zhuǎn)載聲明:版權(quán)規(guī)文章原創(chuàng)作者所有
轉(zhuǎn)載時(shí)間:2007年07月17日 轉(zhuǎn)載作者:pablo3518
HTTP協(xié)議是目前互聯(lián)網(wǎng)上最重要的協(xié)議,許多軟件與服務(wù)都需要依賴HTTP協(xié)議。 import java.io.InputStream;
import java.io.InputStreamReader; iimport java.io.UnsupportedEncodingException; import java.util.*; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HostConfiguration; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpConnection; import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; /** * @author steven */ public class HttpClientExample { //獲得ConnectionManager,設(shè)置相關(guān)參數(shù) private static MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager(); private static int connectionTimeOut = 20000; private static int socketTimeOut = 10000; private static int maxConnectionPerHost = 5; private static int maxTotalConnections = 40; //標(biāo)志初始化是否完成的flag private static boolean initialed = false; //初始化ConnectionManger的方法 public static void SetPara() { manager.getParams().setConnectionTimeout(connectionTimeOut); manager.getParams().setSoTimeout(socketTimeOut); manager.getParams() .setDefaultMaxConnectionsPerHost(maxConnectionPerHost); manager.getParams().setMaxTotalConnections(maxTotalConnections); initialed = true; }
//通過(guò)get方法獲取網(wǎng)頁(yè)內(nèi)容 public static String getGetResponseWithHttpClient(String url, String encode) { HttpClient client = new HttpClient(manager);
if (initialed) { HttpClientExample.SetPara();
} GetMethod get = new GetMethod(url); get.setFollowRedirects(true); String result = null;
StringBuffer resultBuffer = new StringBuffer(); try { client.executeMethod(get); //在目標(biāo)頁(yè)面情況未知的條件下,不推薦使用getResponseBodyAsString()方法
//String strGetResponseBody = post.getResponseBodyAsString(); BufferedReader in = new BufferedReader( new InputStreamReader( get.getResponseBodyAsStream(), get.getResponseCharSet())); String inputLine = null; while ((inputLine = in.readLine()) != null) { resultBuffer.append(inputLine); resultBuffer.append("\n");
} in.close(); result = resultBuffer.toString(); //iso-8859-1 is the default reading encode result = HttpClientExample.ConverterStringCode(resultBuffer.toString(), get.getResponseCharSet(), encode); } catch (Exception e) { e.printStackTrace(); result = ""; } finally {
get.releaseConnection(); return result; } } public static String getPostResponseWithHttpClient(String url, String encode) { HttpClient client = new HttpClient(manager); if (initialed) { HttpClientExample.SetPara(); } PostMethod post = new PostMethod(url);
post.setFollowRedirects(false); StringBuffer resultBuffer = new StringBuffer(); String result = null; try { client.executeMethod(post); BufferedReader in = new BufferedReader( new InputStreamReader(
post.getResponseBodyAsStream(), post.getResponseCharSet())); String inputLine = null; while ((inputLine = in.readLine()) != null) {
resultBuffer.append(inputLine); resultBuffer.append("\n");
} in.close(); //iso-8859-1 is the default reading encode result = HttpClientExample.ConverterStringCode(resultBuffer.toString(), post.getResponseCharSet(), encode); } catch (Exception e) { e.printStackTrace(); result = ""; } finally {
post.releaseConnection(); return result;
} } public static String getPostResponseWithHttpClient(String url, String encode, NameValuePair[] nameValuePair) { HttpClient client = new HttpClient(manager); if (initialed) { HttpClientExample.SetPara(); } PostMethod post = new PostMethod(url);
post.setRequestBody(nameValuePair); post.setFollowRedirects(false); String result = null; StringBuffer resultBuffer = new StringBuffer(); try { client.executeMethod(post); BufferedReader in = new BufferedReader( new InputStreamReader( post.getResponseBodyAsStream(), post.getResponseCharSet())); String inputLine = null; while ((inputLine = in.readLine()) != null) { resultBuffer.append(inputLine);
resultBuffer.append("\n"); } in.close(); //iso-8859-1 is the default reading encode result = HttpClientExample.ConverterStringCode(resultBuffer.toString(), post.getResponseCharSet(), encode); } catch (Exception e) { e.printStackTrace(); result = "";
} finally { post.releaseConnection(); return result; } } private static String ConverterStringCode(String source, String srcEncode, String destEncode) {
if (src != null) { try { return new String(src.getBytes(srcEncode), destEncode); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); return ""; } } else { return ""; }
} } -------------------------------------------------------------------------------- 之后,就可以通過(guò)下面的代碼獲得目標(biāo)網(wǎng)頁(yè): String source = HttpClientExample.getGetResponseWithHttpClient("www.sina.com.cn", "GBK"); 注意,在默認(rèn)情況下,HttpClient的Request的Head中 User-Agent的值是Jakarta Commons-HttpClient 3.0RC1,如果需要改變它(例如,變?yōu)镸ozilla/4.0),必須在調(diào)用之前運(yùn)行如下語(yǔ)句: System.getProperties().setProperty("httpclient.useragent", "Mozilla/4.0"); |
|
來(lái)自: pablo3518 > 《我的圖書(shū)館》