Jsoup系列學習(1)-發送get或post請求


簡介

jsoup 是一款Java 的HTML解析器,可直接解析某個URL地址、HTML文本內容。它提供了一套非常省力的API,可通過DOM,CSS以及類似於jQuery的操作方法來取出和操作數據。

官網:http://www.open-open.com/jsoup/parsing-a-document.htm

1、jsoup的主要功能如下:

1. 從一個URL,文件或字符串中解析HTML;
2. 使用DOM或CSS選擇器來查找、取出數據;
3. 可操作HTML元素、屬性、文本;
jsoup是基於MIT協議發布的,可放心使用於商業項目。

2、jsoup包

1.所使用到的jar包:jsoup-*.jar
        <!-- jsoup包依賴 -->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.8.3</version>
        </dependency>

get請求

1、jsoup模擬get請求還是很簡單的,這里使用它,目的就是來做接口自動化測試,代碼既簡潔又簡單。
參數中有cookie的目的就是適合所有cookie請求,請求中有cookie的傳入cookie值,沒有的傳入空值即可。
    public static String httpGet(String url,String cookie) throws IOException{
        //獲取請求連接
        Connection con = Jsoup.connect(url);
        //請求頭設置,特別是cookie設置
        con.header("Accept", "text/html, application/xhtml+xml, */*"); 
        con.header("Content-Type", "application/x-www-form-urlencoded");
        con.header("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0))"); 
        con.header("Cookie", cookie);
        //解析請求結果
        Document doc=con.get(); 
        //獲取標題
        System.out.println(doc.title());
     //返回內容
return doc.toString(); }

 2、其中get請求參數中,還可以通過另一種方式:

        //獲取請求連接
        Connection conn = Jsoup.connect("http://www.cnblogs.com/zhangfei/p/");
        //請求參數設置
        conn.data("page","3");
        //獲取請求結果
        Document doc = conn.get();       

 3、在發送請求中,我們不光只想獲取響應內容,還想獲取頭信息或者cookie值,例如:在登陸中,我們獲取登陸cookie值,那么我們可以在以后一定時間內發送請求,帶上cookie值,就可以繞過登陸,不用重新登陸。

要取得cookies,必須要有個Response的對象,所以,要用execute方法,如果直接用post方面,返回的則是Document對象,但在用execute方法時,要事先調用一下method方法設定好請求方式即可。

獲取get請求后指定頭文件名稱的值方法:

public static String httpGetHeader(String url,String cook,String header) throws IOException{
        //獲取請求連接
        Connection con = Jsoup.connect(url);
        //請求頭設置,特別是cookie設置
        con.header("Accept", "text/html, application/xhtml+xml, */*"); 
        con.header("Content-Type", "application/x-www-form-urlencoded");
        con.header("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0))"); 
        con.header("Cookie", cook);
        //發送請求
        Response resp=con.method(Method.GET).execute();        
        //獲取cookie名稱為__bsi的值
        String cookieValue = resp.cookie("__bsi");
        System.out.println("cookie  __bsi值:  "+cookieValue);
        //獲取返回cookie所值
        Map<String,String> cookies = resp.cookies();
        System.out.println("所有cookie值:  "+cookies);
        //獲取返回頭文件值
        String headerValue = resp.header(header);
        System.out.println("頭文件"+header+"的值:"+headerValue);
        //獲取所有頭文件值
        Map<String,String> headersOne =resp.headers();
        System.out.println("所有頭文件值:"+headersOne);
        return headerValue; 
        
    }

post請求

1、使用jsoup模擬post請求返回body:

public static String httpPost(String url,Map<String,String> map,String cookie) throws IOException{
        //獲取請求連接
        Connection con = Jsoup.connect(url);
        //遍歷生成參數
        if(map!=null){
            for (Entry<String, String> entry : map.entrySet()) {     
               //添加參數
                con.data(entry.getKey(), entry.getValue());
               } 
        }
        //插入cookie(頭文件形式)
        con.header("Cookie", cookie);
        Document doc = con.post();  
        System.out.println(doc);
        return doc.toString();
    }

 2、發送post請求獲取cookie值獲取headers與get類似:

        //發送請求
        Response resp=con.method(Method.POST).execute();        
        //獲取cookie名稱為__bsi的值
        String cookieValue = resp.cookie(header);
        System.out.println(cookieValue);

 2、源代碼鏈接

鏈接:http://files.cnblogs.com/files/airsen/JsoupUtil.rar

參考

1、jsoup實現爬蟲網絡:http://blog.csdn.net/column/details/jsoup.html

2、Jsoup做接口測試:http://www.cnblogs.com/zhangfei/p/4359408.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM