這期內容當中小編將會給大家帶來有關在java web項目中使用 HttpClient模擬瀏覽器,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
HttpClient模擬瀏覽器登錄后發起請求
瀏覽器實現這個效果需要如下幾個步驟:
1請求一個需要登錄的頁面或資源
2服務器判斷當前的會話是否包含已登錄信息。如果沒有登錄重定向到登錄頁面
3手工在登錄頁面錄入正確的賬戶信息并提交
4服務器判斷登錄信息是否正確,如果正確則將登錄成功信息保存到session中
5登錄成功后服務器端給瀏覽器返回會話的SessionID信息保存到客戶端的Cookie中
6瀏覽器自動跳轉到之前的請求地址并攜帶之前的Cookie(包含登錄成功的SessionID)
7服務器端判斷session中是否有成功登錄信息,如果有則將請求的資源反饋給瀏覽器
package com.artsoft.demo;
import java.io.FileOutputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.util.EntityUtils;
/**
* TODO(用一句話描述該文件的作用)
*
* @title: HttpClientDemo.java
* @author zhangjinshan-ghq
* @date 2014-6-11 14:59:04
*/
public class HttpClientDemo
{
/**
* The main method.
*
* @param args the arguments
* @throws Exception the exception
*/
public static void main(String[] args) throws Exception
{
getResoucesByLoginCookies();
}
/**
* 根據登錄Cookie獲取資源
* 一切異常均未處理,需要酌情檢查異常
*
* @throws Exception
*/
private static void getResoucesByLoginCookies() throws Exception
{
HttpClientDemo demo = new HttpClientDemo();
String username = "......";// 登錄用戶
String password = "......";// 登錄密碼
// 需要提交登錄的信息
String urlLogin = "http://hx.buscoming.cn/Api/Security/Logon?UserCode=" + username + "&Password=" + password;
// 登錄成功后想要訪問的頁面 可以是下載資源 需要替換成自己的iteye Blog地址
String urlAfter = "http://hx.buscoming.cn/Api/Security/GetLoginAccount";
DefaultHttpClient client = new DefaultHttpClient(new PoolingClientConnectionManager());
/**
* 第一次請求登錄頁面 獲得cookie
* 相當于在登錄頁面點擊登錄,此處在URL中 構造參數,
* 如果參數列表相當多的話可以使用HttpClient的方式構造參數
* 此處不贅述
*/
HttpPost post = new HttpPost(urlLogin);
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
CookieStore cookieStore = client.getCookieStore();
client.setCookieStore(cookieStore);
/**
* 帶著登錄過的cookie請求下一個頁面,可以是需要登錄才能下載的url
* 此處使用的是iteye的博客首頁,如果登錄成功,那么首頁會顯示【歡迎XXXX】
*
*/
HttpGet get = new HttpGet(urlAfter);
response = client.execute(get);
entity = response.getEntity();
/**
* 將請求結果放到文件系統中保存為 myindex.html,便于使用瀏覽器在本地打開 查看結果
*/
String pathName = "d:\\index.html";
writeHTMLtoFile(entity, pathName);
}
/**
* Write htmL to file.
* 將請求結果以二進制形式放到文件系統中保存為.html文件,便于使用瀏覽器在本地打開 查看結果
*
* @param entity the entity
* @param pathName the path name
* @throws Exception the exception
*/
public static void writeHTMLtoFile(HttpEntity entity, String pathName) throws Exception
{
byte[] bytes = new byte[(int) entity.getContentLength()];
FileOutputStream fos = new FileOutputStream(pathName);
bytes = EntityUtils.toByteArray(entity);
fos.write(bytes);
fos.flush();
fos.close();
}
}
上述就是小編為大家分享的在java web項目中使用 HttpClient模擬瀏覽器了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。