免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
利用HttpClient獲取網(wǎng)頁內(nèi)容
轉(zhuǎn)載聲明:版權規(guī)文章原創(chuàng)作者所有
轉(zhuǎn)載時間:2007年07月17日
轉(zhuǎn)載作者:pablo3518

HTTP協(xié)議是目前互聯(lián)網(wǎng)上最重要的協(xié)議,許多軟件與服務都需要依賴HTTP協(xié)議。
雖然java.net這個package中包含了對HTTP的基本支持,但還有很多高級和復雜的功能無法實現(xiàn),這不能不說是一個遺憾。
HttpClient作為Apache的開源項目項目之一,為基于HTTP協(xié)議的操作提供了強大的客戶端執(zhí)行支持,最新的版本為3.0RC3。
下面通過一個例子簡要展示HttpClient的使用方法:

--------------------------------------------------------------------------------
import java.io.BufferedReader;
import java.io.IOException;

JAVA手機網(wǎng)[www.cnjm.net]
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ù)
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;

//標志初始化是否完成的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;
JAVA手機網(wǎng)[www.cnjm.net]
}

//通過get方法獲取網(wǎng)頁內(nèi)容
public static String getGetResponseWithHttpClient(String url, String encode) {
JAVA手機網(wǎng)[www.cnjm.net]
HttpClient client = new HttpClient(manager);

if (initialed) {
JAVA手機網(wǎng)[www.cnjm.net]
HttpClientExample.SetPara();
}

GetMethod get = new GetMethod(url);
get.setFollowRedirects(true);

JAVA手機網(wǎng)[www.cnjm.net]
String result = null;
StringBuffer resultBuffer = new StringBuffer();

try {

client.executeMethod(get);
JAVA手機網(wǎng)[www.cnjm.net]

JAVA手機網(wǎng)[www.cnjm.net]
//在目標頁面情況未知的條件下,不推薦使用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);
JAVA手機網(wǎng)[www.cnjm.net]
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 = "";
JAVA手機網(wǎng)[www.cnjm.net]
} finally {
get.releaseConnection();

return result;
}
}

public static String getPostResponseWithHttpClient(String url,
String encode) {
HttpClient client = new HttpClient(manager);
JAVA手機網(wǎng)[www.cnjm.net]

if (initialed) {
HttpClientExample.SetPara();
}

JAVA手機網(wǎng)[www.cnjm.net]
PostMethod post = new PostMethod(url);
post.setFollowRedirects(false);

StringBuffer resultBuffer = new StringBuffer();

String result = null;

try {
client.executeMethod(post);

BufferedReader in = new BufferedReader(
JAVA手機網(wǎng)[www.cnjm.net]
new InputStreamReader(
post.getResponseBodyAsStream(),
post.getResponseCharSet()));
String inputLine = null;

JAVA手機網(wǎng)[www.cnjm.net]
while ((inputLine = in.readLine()) != null) {
resultBuffer.append(inputLine);
JAVA手機網(wǎng)[www.cnjm.net]
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 = "";
JAVA手機網(wǎng)[www.cnjm.net]
} finally {
post.releaseConnection();

JAVA手機網(wǎng)[www.cnjm.net]
return result;
}
}

public static String getPostResponseWithHttpClient(String url,
String encode,
NameValuePair[] nameValuePair) {
HttpClient client = new HttpClient(manager);

if (initialed) {
HttpClientExample.SetPara();
}

JAVA手機網(wǎng)[www.cnjm.net]
PostMethod post = new PostMethod(url);
post.setRequestBody(nameValuePair);
post.setFollowRedirects(false);
JAVA手機網(wǎng)[www.cnjm.net]

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) {
JAVA手機網(wǎng)[www.cnjm.net]
resultBuffer.append(inputLine);
resultBuffer.append("\n");
}

in.close();
JAVA手機網(wǎng)[www.cnjm.net]


//iso-8859-1 is the default reading encode
result = HttpClientExample.ConverterStringCode(resultBuffer.toString(),
post.getResponseCharSet(),
encode);
} catch (Exception e) {
e.printStackTrace();

JAVA手機網(wǎng)[www.cnjm.net]
result = "";
} finally {
post.releaseConnection();

return result;
}
}

JAVA手機網(wǎng)[www.cnjm.net]
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 "";
JAVA手機網(wǎng)[www.cnjm.net]
}
}
}

--------------------------------------------------------------------------------
之后,就可以通過下面的代碼獲得目標網(wǎng)頁:
String source = HttpClientExample.getGetResponseWithHttpClient("www.sina.com.cn", "GBK");

注意,在默認情況下,HttpClient的Request的Head中
User-Agent的值是Jakarta Commons-HttpClient 3.0RC1,如果需要改變它(例如,變?yōu)镸ozilla/4.0),必須在調(diào)用之前運行如下語句:
System.getProperties().setProperty("httpclient.useragent", "Mozilla/4.0");
本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Apache Commons工具集簡介
JAVA利用httpclient登錄開心網(wǎng)
httpclient教程
Android中使用HTTP服務
用Java和Perl登錄新浪微博
抓取一個網(wǎng)站特定的全部圖片(JAVA)
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服