|
|
@@ -7,9 +7,11 @@ import okhttp3.*;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
@@ -24,17 +26,18 @@ import java.util.regex.Pattern;
|
|
|
public class HttpUtils {
|
|
|
|
|
|
|
|
|
- /**okhttp 实例**/
|
|
|
- private static OkHttpClient client = new OkHttpClient();;
|
|
|
-// private static CloseableHttpClient client = HttpClients.custom()
|
|
|
-// .build();
|
|
|
+ /**
|
|
|
+ * okhttp 实例
|
|
|
+ **/
|
|
|
+ private static OkHttpClient client = MyOkHttpClient.getClient();
|
|
|
|
|
|
/**解析键值对正则**/
|
|
|
public static final Pattern PAT_KEY_VALUE = Pattern.compile("[\\w]*=[\\w,/]*");
|
|
|
|
|
|
|
|
|
public static String doGet(String url, Map<String, String> params,String cookie) {
|
|
|
- Request.Builder reqBuild = new Request.Builder();
|
|
|
+ Request.Builder reqBuild = new Request.Builder()
|
|
|
+ .addHeader("Cookie", cookie);
|
|
|
HttpUrl.Builder urlBuilder = HttpUrl.parse(url)
|
|
|
.newBuilder();
|
|
|
if (params != null) {
|
|
|
@@ -133,15 +136,58 @@ public class HttpUtils {
|
|
|
Response response = null;
|
|
|
try {
|
|
|
response = client.newCall(request).execute();
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ List<String> cookies = response.headers("set-cookie");
|
|
|
+ System.out.println(cookies);
|
|
|
+ for (String s : cookies) {
|
|
|
+ String[] split = s.split(";");
|
|
|
+ list.add(split[0]);
|
|
|
+ }
|
|
|
if (!response.isSuccessful()) {
|
|
|
throw new RuntimeException("请求不成功" + response.toString());
|
|
|
}
|
|
|
+ String string = response.body().string();
|
|
|
return response.body().string();
|
|
|
} catch (IOException e) {
|
|
|
throw new RuntimeException(e.getMessage(), e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public static Map<String, String> getCookies(String url, Map<String, String> params, String cookie) {
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ FormBody.Builder formBuilder = new FormBody.Builder();
|
|
|
+ if (params != null) {
|
|
|
+ params.forEach(formBuilder::addEncoded);
|
|
|
+ }
|
|
|
+
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(url)
|
|
|
+ .post(formBuilder.build())
|
|
|
+ .addHeader("Cookie", cookie)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ Response response = null;
|
|
|
+ try {
|
|
|
+ response = client.newCall(request).execute();
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ List<String> cookies = response.headers("set-cookie");
|
|
|
+ System.out.println(cookies);
|
|
|
+ for (String s : cookies) {
|
|
|
+ String[] split = s.split(";");
|
|
|
+ list.add(split[0]);
|
|
|
+ }
|
|
|
+ map.put("cookies", StringUtils.join(list, ";"));
|
|
|
+ if (!response.isSuccessful()) {
|
|
|
+ throw new RuntimeException("请求不成功" + response.toString());
|
|
|
+ }
|
|
|
+ String str = response.body().string();
|
|
|
+ map.put("boby", str);
|
|
|
+ return map;
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* post
|
|
|
* @return
|