非常便捷的网络请求工具----JsoupUtil

JsonpUtil是一个非常便捷的网络访问及爬虫工具类。废话不多说...上代码.
1.导入maven依赖jar包

  
    
     
     org.jsoup
     jsoup
     1.12.1
    
    

2.创建以下JsoupUtil

  package com.traffic.server.utils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
import java.util.Map;
/**
 * @program: traffic_resource
 * @description
 * @author: belive
 * @create: 2021-01-08 15:41
 **/
 public class JsoupUtil {
    public static Document sendGet(String url, Integer timeOut) throws IOException {
        return Jsoup.connect(url).timeout(timeOut).ignoreContentType(true)
                .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
                .header("Accept-Encoding", "gzip, deflate, sdch")
                .header("Accept-Language", "zh-CN,zh;q=0.8")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36")
                .get();
 }
    public static Document sendPost(String url, Integer timeOut, Map map) throws IOException {
        return Jsoup.connect(url).timeout(timeOut).ignoreContentType(true)
                .header("Accept", "application/json, text/javascript, */*; q=0.01")
                .header("Accept-Encoding", "gzip, deflate")
                .header("Accept-Language", "zh-CN,zh;q=0.9")
                .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36")
                .data(map)
                .post();
 }
    public static Document sendPostByBody(String url, Integer timeOut, String json) throws IOException {
        return Jsoup.connect(url).timeout(timeOut).ignoreContentType(true)
                .header("Accept", "application/json, text/javascript, */*; q=0.01")
                .header("Accept-Encoding", "gzip, deflate")
                .header("Accept-Language", "zh-CN,zh;q=0.9")
                .header("Content-Type", "application/json;charset=utf-8")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36")
                .requestBody(json)
                .post();
 }  
 
 

你可能感兴趣的:(java)