http篇记录:包括jsoup调用接口
网页直接调用 请求 传入json参数: %7B :{ %22:" %7D:} localhost:8080/xxx/xxx/save?id=5&json=%7B%22name%22:%22name%22,%22result%22:%22%22%7D
jsoup调用接口
Connection con=Jsoup.connect("http://192.168.111.111:8080/api/xxxxx"); con.data("pwd","123456"); con.ignoreContentType(true); String res=con.post().text();
jsoup 调用接口的方法: post 传参json 。
Connection con = Jsoup.connect(callurl) .requestBody(str) .userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)") .ignoreContentType(true) .header("Content-Type", "application/json"); Document doc = con.timeout(50000).post(); res=doc.text(); if(res != null){ result.setSuccess(true); result.setReturnMsg("success"); result.setData(res); }else { result.setSuccess(false); result.setReturnMsg("error"); }
jsoup 调用get方法: 并设置请求头:
public static String getCheckDataState(String userToken,String dataId){ String callurl = ResourceBundle.getBundle("application").getString("checkData"); callurl = callurl+userToken; String res=""; try{ Connection con = Jsoup.connect(callurl) .ignoreContentType(true) .ignoreHttpErrors(true) .timeout(1000 * 30) .userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36") .header("accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8") .header("accept-encoding","gzip, deflate, br") .header("accept-language","zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7"); con.data("dataID",dataId); Document doc = con.timeout(50000).get(); res=doc.text(); }catch (Exception e){ e.printStackTrace(); } return res; }
解决jsoup 调用 请求超时的错误: 设置.timeout属性。
Document doc = con.timeout(50000).get();
上一篇:
Python 安装包管理工具 pip
下一篇:
IDEA配置代码注释模板