1 依赖
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.13.1</version>
</dependency>
2 Get 请求
2.1 HTML
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
...
public JSONObject doGet(String paramUrl) {
try {
Document doc = Jsoup.connect(paramUrl)
.ignoreContentType(true)
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36 QBCore/4.0.1295.400 QQBrowser/9.0.2524.400 Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2875.116 Safari/537.36 NetType/WIFI MicroMessenger/7.0.5 WindowsWechat")
.timeout(10000)
.get();
String returnStr = doc.getElementsByClass("标签名").text();
JSONObject reqResJson = changeJson(returnStr);
return reqResJson;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
2.2 Json
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
...
public JSONObject doGet(String url) {
try {
Response response = Jsoup.connect(url)
.ignoreContentType(true)
.header("Content-type", "application/x-www-form-urlencoded; charset=UTF-8")
.header("x-forwarded-for", IpUtil.getRandomIp())
.method(Method.GET)
.timeout(10000)
.execute();
if (response.statusCode() == 200) {
String bodyStr = response.body();
return JSONObject.parseObject(bodyStr);
}
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
3 Post 请求
3.1 application/x-www-form-urlencoded
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
public JSONObject doPost(Map<String, String> param, String url) {
try {
Response response = Jsoup.connect(url)
.ignoreContentType(true)
.header("Content-type", "application/x-www-form-urlencoded")
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
.header("x-forwarded-for", IpUtil.getRandomIp())
.method(Method.POST)
.data(param)
.timeout(25000)
.execute();
if (response.statusCode() == 200) {
String body = response.body();
return JSONObject.parseObject(body);
}
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
3.2 text/plain
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
public JSONObject doPost(String param, String url) {
try {
Response response = Jsoup.connect(url)
.ignoreContentType(true)
.header("Content-type", "text/plain")
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
.header("x-forwarded-for", IpUtil.getRandomIp())
.method(Method.POST)
.requestBody(param)
.timeout(15000)
.execute();
if (response.statusCode() == 200) {
String body = response.body();
return JSONObject.parseObject(body);
}
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
3.3 application/json
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
...
public JSONObject doPost(String paramJsonStr, Proxy proxy, url) {
try {
Response response = Jsoup.connect(url)
.ignoreContentType(true)
.header("Content-Type", "application/json;charset=UTF-8")
.header("User-Agent", "Mozilla/5.0 (Linux; Android 5.1.1; sm-j200g Build/LMY48Z) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/39.0.0.0 Mobile Safari/537.36")
.requestBody(paramJsonStr)
.proxy(proxy)
.method(Method.POST)
.timeout(10000)
.execute();
if (response.statusCode() == 200) {
String bodyStr = response.body();
return JSONObject.parseObject(bodyStr);
}
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
3.4 HTML
private JSONObject doPost(Map<String, String> param, String cookie) {
try {
Document doc = Jsoup.connect("http://*****").ignoreContentType(true)
.header("Content-Type", "application/x-www-form-urlencoded")
.userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36 QBCore/4.0.1301.400 QQBrowser/9.0.2524.400 Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2875.116 Safari/537.36 NetType/WIFI MicroMessenger/7.0.5 WindowsWechat")
.data(param)
.header("cookie", cookie)
.timeout(10000)
.post();
JSONObject reqResJson = new JSONObject();
Elements elements = doc.getElementsByTag("li");
reqResJson = change(elements, reqResJson);
return reqResJson;
}
catch (Exception e) {
}
return reqResJson
}
4 IpUtil
import java.util.Random;
public class IpUtil {
private static final int[][] RANGE = {
{ 607649792, 608174079 },
{ 1038614528, 1039007743 },
{ 1783627776, 1784676351 },
{ 2035023872, 2035154943 },
{ 2078801920, 2079064063 },
{ -1950089216, -1948778497 },
{ -1425539072, -1425014785 },
{ -1236271104, -1235419137 },
{ -770113536, -768606209 },
{ -569376768, -564133889 },
};
public static String getRandomIp() {
int index = new Random().nextInt(10);
return num2ip(RANGE[index][0] + new Random().nextInt(RANGE[index][1] - RANGE[index][0]));
}
public static String num2ip(int ip) {
int[] b = new int[4];
b[0] = ((ip >> 24) & 0xff);
b[1] = ((ip >> 16) & 0xff);
b[2] = ((ip >> 8) & 0xff);
b[3] = (ip & 0xff);
return b[0] + "." + b[1] + "." + b[2] + "." + b[3];
}
}