场景:你需要使用代理才能使用http
你需要引入5.4.6的版本才拥有setHttpProxy这个方法
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.6</version>
</dependency>
String result = HttpUtil.createGet(url).setHttpProxy("56.1.XX", xxx).execute().body();
情况二没办法避免需要使用低版本:
//如果hutool版本过低则需要代理类
Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(host, port));
HttpUtil.createPost("www.baidu.com").setProxy(proxy );
hutool json格式发送请求方法(设置代理)
//此处需要引入hutool的JSONObject对象
JSONObject json = new JSONObject();
json.putOpt("hanqitao","hanqitao");
json.putOpt("lyy","lyy");
//发送json请求包
String body = HttpUtil.createPost("www.baidu.com").body(json.toString()).setHttpProxy("xxx.xx", xx).execute().body();
System.out.println(body);
hutool json格式发送请求方法(不设置代理)
JSONObject json = new JSONObject();
json.putOpt("hanqitao","hanqitao");
json.putOpt("lyy","lyy");
String post = HttpUtil.post("www.baidu.com", json.toString());
System.out.println(post);