Jsoup请求网络

前言

使用了很久的jsonp了,把项目中用到的几个方法总结下吧

<!-- jsoup包依赖 -->
<dependency>
	<groupId>org.jsoup</groupId>
	<artifactId>jsoup</artifactId>
	<version>1.11.1</version>
</dependency>

<!--json-->
<dependency>
	<groupId>net.sf.json-lib</groupId>
	<artifactId>json-lib</artifactId>
	<version>2.4</version>
	<classifier>jdk15</classifier>
</dependency>

1、Get请求

1.1、返回结果为Document

Document document = Jsoup.connect(url).
                    ignoreContentType(true).
                    get();
String result = document.body().text();


1.2、返回结果为Response

Connection.Response 
connection = Jsoup.connect("http://www.baidu.com")
             .ignoreContentType(true)
            .data(data)
            .headers(headers)
            .method(Connection.Method.GET)
            .validateTLSCertificates(false)
            .execute();

 String body =  connection.body();
        

2、Post请求


  Jsoup.connect("https://oapi.dingtalk.com/robot/send?access_token=7f6b0788cc5137e7cb1a73f0e5b6d5663aefbbcc40f2e9ac513d7bb84d2bc31f")
          .ignoreContentType(true)
          .header("Content-Type", "application/json; charset=utf-8")
          .requestBody("{\"msgtype\": \"text\",\"text\": { \"content\": \""+text+"\" } }")
          .post();






感兴趣的,欢迎添加博主微信,

哈,博主很乐意和各路好友交流,如果满意,请打赏博主任意金额,感兴趣的在微信转账的时候,备注您的微信或者其他联系方式。添加博主微信哦。


请下方留言吧。可与博主自由讨论哦

微信 微信公众号 支付宝
微信 微信公众号 支付宝
posted @ 2019-01-17 18:17  HealerJean  阅读(136)  评论(0编辑  收藏  举报