Jsoup请求网络
前言
Github:https://github.com/HealerJean
1、依赖
<!-- 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>
2、返回结果的不同
关键点在于:ignoreContentType(true) ,这个是忽略请求类型
get请求
<body>
{"statusCode":"100001","desc":"验证失败","result":[]}
</body>
execute请求
{"statusCode":"100001","desc":"验证失败","result"
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/")
.ignoreContentType(true)
.header("Content-Type", "application/json; charset=utf-8")
.requestBody("{\"msgtype\": \"text\",\"text\": { \"content\": \""+text+"\" } }")
.post();