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

原文地址:https://www.cnblogs.com/airsen/p/6135238.html