溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

在java web項(xiàng)目中使用 HttpClient模擬瀏覽器

發(fā)布時(shí)間:2020-11-20 15:58:00 來(lái)源:億速云 閱讀:492 作者:Leah 欄目:編程語(yǔ)言

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)在java web項(xiàng)目中使用 HttpClient模擬瀏覽器,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

HttpClient模擬瀏覽器登錄后發(fā)起請(qǐng)求

瀏覽器實(shí)現(xiàn)這個(gè)效果需要如下幾個(gè)步驟:

    1請(qǐng)求一個(gè)需要登錄的頁(yè)面或資源

    2服務(wù)器判斷當(dāng)前的會(huì)話是否包含已登錄信息。如果沒(méi)有登錄重定向到登錄頁(yè)面

    3手工在登錄頁(yè)面錄入正確的賬戶信息并提交

    4服務(wù)器判斷登錄信息是否正確,如果正確則將登錄成功信息保存到session中

    5登錄成功后服務(wù)器端給瀏覽器返回會(huì)話的SessionID信息保存到客戶端的Cookie中

    6瀏覽器自動(dòng)跳轉(zhuǎn)到之前的請(qǐng)求地址并攜帶之前的Cookie(包含登錄成功的SessionID)

    7服務(wù)器端判斷session中是否有成功登錄信息,如果有則將請(qǐng)求的資源反饋給瀏覽器

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(); 
  } 
 
  /** 
   * 根據(jù)登錄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; 
 
    // 登錄成功后想要訪問(wèn)的頁(yè)面 可以是下載資源 需要替換成自己的iteye Blog地址 
    String urlAfter = "http://hx.buscoming.cn/Api/Security/GetLoginAccount";
 
    DefaultHttpClient client = new DefaultHttpClient(new PoolingClientConnectionManager()); 
 
    /** 
     * 第一次請(qǐng)求登錄頁(yè)面 獲得cookie 
     * 相當(dāng)于在登錄頁(yè)面點(diǎn)擊登錄,此處在URL中 構(gòu)造參數(shù), 
     * 如果參數(shù)列表相當(dāng)多的話可以使用HttpClient的方式構(gòu)造參數(shù) 
     * 此處不贅述 
     */ 
    HttpPost post = new HttpPost(urlLogin); 
    HttpResponse response = client.execute(post); 
    HttpEntity entity = response.getEntity(); 
    CookieStore cookieStore = client.getCookieStore(); 
    client.setCookieStore(cookieStore); 
 
    /** 
     * 帶著登錄過(guò)的cookie請(qǐng)求下一個(gè)頁(yè)面,可以是需要登錄才能下載的url 
     * 此處使用的是iteye的博客首頁(yè),如果登錄成功,那么首頁(yè)會(huì)顯示【歡迎XXXX】 
     * 
     */ 
    HttpGet get = new HttpGet(urlAfter); 
    response = client.execute(get); 
    entity = response.getEntity(); 
 
    /** 
     * 將請(qǐng)求結(jié)果放到文件系統(tǒng)中保存為 myindex.html,便于使用瀏覽器在本地打開(kāi) 查看結(jié)果 
     */ 
 
    String pathName = "d:\\index.html"; 
    writeHTMLtoFile(entity, pathName); 
  } 
 
  /** 
   * Write htmL to file. 
   * 將請(qǐng)求結(jié)果以二進(jìn)制形式放到文件系統(tǒng)中保存為.html文件,便于使用瀏覽器在本地打開(kāi) 查看結(jié)果 
   * 
   * @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項(xiàng)目中使用 HttpClient模擬瀏覽器了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI