3.1.4 Jsoup提交请求参数

本文介绍了使用Jsoup库向指定URL提交POST请求的方法,详细展示了如何设置请求参数,包括直接添加参数、使用Map集合设置参数等不同方式,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网页提交表单数据,涉及到一系列请求参数。GET请求的参数,是通过URL传递的,通常以“?key1=value1&key2=value2”的形式进行传递。 POST请求的参数,通常是放在POST请求的消息体中,格式一般为JSON。例如,在某快递网站中输入快递单号查询快递信息,通过网络抓包获取的请求信息如下图所示,请求的方法为POST,提交的参数有2个。
请求地址:https://www.kuaidi100.com/autonumber/autoComNum。
快递单号:73123917441103。
抓包获得的参数:resultv2:1 text:73123917441103。
在这里插入图片描述
Jsoup主要提供了以下3种提交请求参数的方法。

//程序3-5
public class JsoupConnectParameter {
    public static void main(String[] args) throws IOException {
        Connection connect = Jsoup.connect("https://www.kuaidi100.com/autonumber/autoComNum");
        //添加参数
        connect.data("resultv2","1").data("text", "73123917441103");
        Connection.Response response = connect.method(Connection.Method.POST).ignoreContentType(true).execute();
        //获取数据,处理成HTML
        Document document = response.parse();
        System.out.println(document);
    }
}
//程序3-6
public class JsoupConnectParameter {
    public static void main(String[] args) throws IOException {
        Connection connect = Jsoup.connect("https://www.kuaidi100.com/autonumber/autoComNum");
        //添加参数
        Map<String, String> data = new HashMap<String, String>();
        data.put("resultv2", "1");
        data.put("text", "73123917441103");
        //获取响应
        Connection.Response response = connect.data(data).method(Connection.Method.POST).ignoreContentType(true).execute();
        //获取数据,处理成HTML
        Document document = response.parse();
        System.out.println(document);
    }
}
//程序3-7
public class JsoupConnectParameter {
    public static void main(String[] args) throws IOException {
        Connection connect = Jsoup.connect("https://www.kuaidi100.com/autonumber/autoComNum");
        //添加参数
        connect.data("resultv2", "1", "text", "73123917441103");
        Connection.Response response = connect.method(Connection.Method.POST).ignoreContentType(true).execute();
        //获取数据,处理成HTML
        Document document = response.parse();
        System.out.println(document);
    }
}

上述3个程序的运行结果如下图所示。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值