HttpClient和HttpURLConnection獲取伺服器返回的內容
HttpClient:
/**
* 獲取賬號密碼介面的網頁原始碼
* @return
*/
private String getAccountPasswordHtml() {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(mAccountPasswordURL);
String content = null;
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
if (200 == httpResponse.getStatusLine().getStatusCode()) {
content = EntityUtils.toString(httpResponse.getEntity());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
HttpURLConnection:
/**
* 獲取網頁原始碼
* @return
*/
private String getHtmlString(String targetUrl) {
String content = null;
HttpURLConnection connection = null;
try {
URL url = new URL(targetUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET" );
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
connection.setRequestProperty("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*");
connection.setRequestProperty("Accept-Language", "zh-cn");
connection.setRequestProperty("UA-CPU", "x86");
//為什麼沒有deflate呢
connection.setRequestProperty("Accept-Encoding", "gzip");
connection.setRequestProperty("Content-type", "text/html");
//keep-Alive,有什麼用呢,你不是在訪問網站,你是在採集。嘿嘿。減輕別人的壓力,也是減輕自己。
connection.setRequestProperty("Connection", "close");
//不要用cache,用了也沒有什麼用,因為我們不會經常對一個連結頻繁訪問。(針對程式)
connection.setUseCaches(false);
connection.setConnectTimeout(6 * 1000);
connection.setReadTimeout(6 * 1000);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Charset", "utf-8");
connection.connect();
if (200 == connection.getResponseCode()) {
InputStream inputStream = null;
if (!TextUtils.isEmpty(connection.getContentEncoding())) {
String encode = connection.getContentEncoding().toLowerCase();
if (!TextUtils.isEmpty(encode) && encode.indexOf("gzip") >= 0) {
inputStream = new GZIPInputStream(connection.getInputStream());
}
}
if (null == inputStream) {
inputStream = connection.getInputStream();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "gbk"));
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
builder.append(line).append("\n");
}
content = builder.toString();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return content;
}
相關推薦
HttpClient和HttpURLConnection獲取伺服器返回的內容
HttpClient: /** * 獲取賬號密碼介面的網頁原始碼 * @return */ private String getAccountPasswordHtml() { HttpClient ht
HttpClient和HttpURLConnection的使用和區別
https://www.cnblogs.com/liushuibufu/p/4140913.html 功能用法對比 從功能上對比,HttpURLConnection比HttpClient庫要豐富很多,提供了很多工具,封裝了http的請求頭,引數,內容體,響應,還有一
HttpClient和HttpURLConnection的區別和案例
HttpURLConnection 物件 1. HttpURLConnection物件需要通過URL類中的openConnection()方法來獲得,它無法直接構造。 HttpsURLConnection urlconn = null; URL url = new UR
HttpClient和HttpURLConnection整合彙總對比
總結了網上的一些資源,主要有以下兩個觀點: 分析一: 在研究Volley框架的原始碼中,發現它在HTTP請求的使用上比較有意思,在Android 2.3及以上版本,使用的是HttpURLConnection,而在Android 2.2及以下版本,使用的是HttpClient。我也比較
HttpClient 和 HttpURLConnection 共用session
最近又開始折騰android專案了,專案中需要請求伺服器段的介面,又要涉及到圖片的資源,比較糾結的是,我開始著手這個專案的時候,網路請求的框架已經完成了,現在存在一個問題就是,請求介面的使用的是HttpClient,這個是已經封裝好了,不方便修改的,但是圖片下載,如果這套
php獲取客戶端IP和php獲取伺服器端IP
1.php獲取客戶端IP 在PHP獲取客戶端IP時,常使用 $_SERVER["REMOTE_ADDR"] 。但如果客戶端是使用代理伺服器來訪問,那取到的是代理伺服器的 IP 地址,而不是真正的客戶端 IP 地址。要想透過代理伺服器取得客戶端的真實 IP 地址,就要
使用Filter獲取伺服器響應內容(字串)
spring mvc架構的web應用中,spring框架將資料model渲染至jsp頁面並將最終結果輸出到客戶端,model和jsp模板可以由程式定義,但是頁面渲染及結果輸出過程是由spring封裝,對程式設計師來說是不可控的。想要取得輸出的內容有一種方法就是自定義ServletOutputStream物件
jquery $.ajax+php使用jsonp處理資料時,前端success :function無法獲取伺服器返回資料
說明:下面描述的過程前端使用的jQuery 前提下完成的 $.ajax+php開發應用時,由於某些原因,ajax必須使用跨域操作處理資料(jsonp)發現 success:function(msg){ alert(msg); } 不執行,但是ajax請求php的資料在後端
HTTP基礎與Android之——使用HttpClient和HttpURLConnection
1、客戶端連線伺服器實現內部的原理: 分析上圖,步驟如下: 第一步:在瀏覽器客戶端中得到使用者輸入的內容。 第二步:瀏覽器得到這個網址之後,內部會將這個域名傳送到DNS上,進行域名解析。得到它的IP之後就會連結到指定的伺服器上,假如伺服器的地址是:221.104.13.32:80,從瀏覽器
贈送 HttpClient 和HttpURLConnection 的輕型網路框架 ---》使用介紹
之前看網上的一些網路框架,多少存在一些bug,大多還沒有原始碼,有些有原始碼,框架過於太大,發現了bug修改起來也實在費事,所以使用自己的網路框架是最佳選擇哦,這個框架我這裡有借鑑一些其他框架的地方,不過提供功能就是 post 請求網路。 Android使用 Ht
HttpClient和HttpURLConnection哪個更好
最近在研究Volley框架的原始碼,發現它在HTTP請求的使用上比較有意思,在Android 2.3及以上版本,使用的是HttpURLConnection,而在Android 2.2及以下版本,使用的是HttpClient。我也比較好奇這麼使用的原因,於是專門找到了一位Google的工程師寫的一篇部落格,
HttpClient和HttpURLConnection的區別
簡單而言HttpClient就是加強版的HttpURLConnection,但是HttpClient能夠維護客戶端和服務端的Session。 宣告:轉載自:http://blog.csdn.net/hguang_zjh/article/details/33743249
HttpClient和HttpURLConnection網路請求資料
public class MainActivity extends Activity { // 全域性 NewsData newsData; private Button btn1, btn2; private ListView list; private MyAda
利用httpClient和htmlParse獲取網頁iframe資料
public static void main(String[] args) { HttpClient client = new HttpClient(); HttpMethod method = new GetMethod("http://www.ln.
HttpClient get和HttpClient Post請求的方式獲取伺服器的返回資料
/* * 演示通過HttpClient get請求的方式獲取伺服器的返回資料 */public class HttpClientDemo { public static void main(String[] args) throws ClientProtocolEx
通過HttpURLConnection連線伺服器,傳送報文,獲取伺服器報文返回
Java 通過HttpURLConnection連線伺服器 傳送 POST 和 GET 請求 package com.dataservice.utils; import java.io.BufferedReader; import java.io.IOException; impo
國際化從伺服器獲取string.xml內容 要考慮的
1、/res 下面的內容不允許執行時修改; 2、針對Android UI 生成特定的string檔案; 3、文字間的空格(比如:<string name="action_sign_in">登\u0020\u0020錄</string>); 4、字串之間的佔位符(比
ajax獲取伺服器靜態資源(一個.json檔案),請求成功,有檔案返回,但是ajax回撥卻進了error(一個回車鍵惹出的禍)
問題描述:同事反應問題:向後臺請求json檔案,完了在前臺渲染一個表格,但是始終渲染不出來。 $.ajax({ &nbs
java實現 連線遠端伺服器 執行Linux命令 並獲取 執行返回的結果
情景:要通過java連線伺服器,並執行指令碼命令 得到 返回的結果 package com.ideal.openapi.util; import com.jcraft.jsch.*; import org.slf4j.Logger; import org.slf4j.LoggerFacto
HttpClient解析伺服器返回的response出現亂碼
引用處: 【問題解決】HttpClient解析伺服器返回的response出現亂碼 問題場景 最近在用httpClient做網路爬蟲的時候,遇到了一個不大不小的問題,當使用HttpGet向指定網址傳送請求後,接收到的Response無法正常解析,出現 口口??這樣的亂碼,編碼也考慮到了中