18910140161

Jsoup请求网络

顺晟科技

2021-06-16 10:47:57

225

前言

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

!- jsoup包依赖-

属国

groupIdorg.jsoup/groupId

artifactIdjsoup/artifactId

版本1 .11 .1/版本

/依赖性

!- json -

属国

groupIdnet.sf.json-lib/groupId

artifactIdjson-lib/artifactId

版本2.4/版本

分类器JDK 15分类器

/依赖性

关键点在于:ignoreContentType(true),这个是忽略请求类型。建议用执行()去执行,如果用得到去执行的话,返回来是一个超文本标记语言页面包裹的JSON,你处理起来稍微有点费劲。返回情况如下:

举例

得到请求

身体

{'statusCode':'100001 ',' desc': '验证失败,' result':[]}

/body

执行请求

{'statusCode':'100001 ',' desc': '验证失败,'结果'

1、获取请求

1.1、返回结果为文档

document document=Jsoup。连接(网址).

ignoreContentType(true).

get();

字符串结果=document.body().text();

1.2、返回结果为反应

连接。反应

connection=Jsoup。connect(' http://www .百度。com’)。ignoreContentType(true)。数据(数据)。标题(标题)。方法(连接。方法。GET)。有效证书证书(假)。execute();

字符串正文=连接。body();

2、岗位请求

js oup。connect(' https://oapi。丁丁说话。com/robot/send?access _ token=7 F6 b 0788 cc 5137 e 7 CB 1a 73 f 0 e5b 6d 5663 aefbccc 40 F2 e 9 AC 513d 7 bb 84 D2 BC 31 f ')。ignoreContentType(true)。标题('内容类型','应用程序/JSON;charset=utf-8 ')。request BOdy(' { \ ' msgtype ' : ' text \ ',\ ' text ' : { \ ' content ' : \ ' ' text ' \ ' } } ')。post();

相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航