add parseCST and fix cookie bug

This commit is contained in:
Looly
2019-10-05 18:18:57 +08:00
parent 476285b302
commit a4515e218b
20 changed files with 198 additions and 120 deletions

View File

@@ -27,7 +27,7 @@ public abstract class HttpBase<T> {
public static final String HTTP_1_1 = "HTTP/1.1";
/**存储头信息*/
protected Map<String, List<String>> headers = new HashMap<String, List<String>>();
protected Map<String, List<String>> headers = new HashMap<>();
/**编码*/
protected Charset charset = CharsetUtil.CHARSET_UTF_8;
/**http版本*/
@@ -90,7 +90,7 @@ public abstract class HttpBase<T> {
if(null != name && null != value){
final List<String> values = headers.get(name.trim());
if(isOverride || CollectionUtil.isEmpty(values)) {
final ArrayList<String> valueList = new ArrayList<String>();
final ArrayList<String> valueList = new ArrayList<>();
valueList.add(value);
headers.put(name.trim(), valueList);
}else {

View File

@@ -1,44 +1,31 @@
package cn.hutool.http;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.CookieManager;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URLStreamHandler;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSocketFactory;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.resource.BytesResource;
import cn.hutool.core.io.resource.FileResource;
import cn.hutool.core.io.resource.MultiFileResource;
import cn.hutool.core.io.resource.MultiResource;
import cn.hutool.core.io.resource.Resource;
import cn.hutool.core.io.resource.*;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.core.util.*;
import cn.hutool.http.cookie.GlobalCookieManager;
import cn.hutool.http.ssl.SSLSocketFactoryBuilder;
import cn.hutool.json.JSON;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSocketFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* http请求类<br>
* Http请求类用于构建Http请求并同步获取结果此类通过CookieManager持有域名对应的Cookie值再次请求时会自动附带Cookie信息
@@ -987,8 +974,6 @@ public class HttpRequest extends HttpBase<HttpRequest> {
.setHttpsInfo(this.hostnameVerifier, this.ssf)//
.setConnectTimeout(this.connectionTimeout)//
.setReadTimeout(this.readTimeout)//
// 自定义Cookie
.setCookie(this.cookie)
// 定义转发
.setInstanceFollowRedirects(this.maxRedirectCount > 0)
// 流方式上传数据
@@ -996,8 +981,13 @@ public class HttpRequest extends HttpBase<HttpRequest> {
// 覆盖默认Header
.header(this.headers, true);
// 读取全局Cookie信息并附带到请求中
GlobalCookieManager.add(this.httpConnection);
if (null != this.cookie) {
// 当用户自定义Cookie时全局Cookie自动失效
this.httpConnection.setCookie(this.cookie);
} else {
// 读取全局Cookie信息并附带到请求中
GlobalCookieManager.add(this.httpConnection);
}
// 是否禁用缓存
if (this.isDisableCache) {
@@ -1193,8 +1183,6 @@ public class HttpRequest extends HttpBase<HttpRequest> {
/**
* 设置表单类型为Multipart文件上传
*
* @return HttpConnection HTTP连接对象
*/
private void setMultipart() {
this.httpConnection.header(Header.CONTENT_TYPE, CONTENT_TYPE_MULTIPART_PREFIX + BOUNDARY, true);

View File

@@ -151,10 +151,9 @@ public class HttpResponse extends HttpBase<HttpResponse> implements Closeable {
*
* @return Cookie列表
* @since 3.1.1
* @see GlobalCookieManager#getCookieManager()
*/
public List<HttpCookie> getCookies() {
return GlobalCookieManager.getCookieManager().getCookieStore().getCookies();
return GlobalCookieManager.getCookies(this.httpConnection);
}
/**
@@ -374,11 +373,10 @@ public class HttpResponse extends HttpBase<HttpResponse> implements Closeable {
try {
this.status = httpConnection.responseCode();
} catch (IOException e) {
if (e instanceof FileNotFoundException) {
// 服务器无返回内容,忽略之
} else {
if (false == (e instanceof FileNotFoundException)) {
throw new HttpException(e);
}
// 服务器无返回内容,忽略之
}
// 读取响应头信息
@@ -410,7 +408,6 @@ public class HttpResponse extends HttpBase<HttpResponse> implements Closeable {
* 读取主体忽略EOFException异常
*
* @param in 输入流
* @return 自身
* @throws IORuntimeException IO异常
*/
private void readBody(InputStream in) throws IORuntimeException {

View File

@@ -7,6 +7,8 @@ import cn.hutool.http.HttpConnection;
import java.io.IOException;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.HttpCookie;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -43,6 +45,17 @@ public class GlobalCookieManager {
return cookieManager;
}
/**
* 获取指定域名下所有Cookie信息
*
* @param conn HTTP连接
* @return Cookie信息列表
* @since 4.6.9
*/
public static List<HttpCookie> getCookies(HttpConnection conn){
return cookieManager.getCookieStore().get(getDomain(conn));
}
/**
* 将本地存储的Cookie信息附带到Http请求中不覆盖用户定义好的Cookie
*
@@ -53,10 +66,10 @@ public class GlobalCookieManager {
// 全局Cookie管理器关闭
return;
}
Map<String, List<String>> cookieHeader;
try {
cookieHeader = cookieManager.get(URLUtil.toURI(conn.getUrl()), new HashMap<String, List<String>>(0));
cookieHeader = cookieManager.get(getDomain(conn), new HashMap<String, List<String>>(0));
} catch (IOException e) {
throw new IORuntimeException(e);
}
@@ -77,9 +90,18 @@ public class GlobalCookieManager {
}
try {
cookieManager.put(URLUtil.toURI(conn.getUrl()), conn.headers());
cookieManager.put(getDomain(conn), conn.headers());
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
/**
* 获取连接的URL中域名信息例如http://www.hutool.cn/aaa/bb.html得到www.hutool.cn
* @param conn HttpConnection
* @return URI
*/
private static URI getDomain(HttpConnection conn){
return URLUtil.getHost(conn.getUrl());
}
}

View File

@@ -29,6 +29,15 @@ public class HttpRequestTest {
String body = HttpRequest.get("https://www.gjifa.com/pc/").execute().body();
Console.log(body);
}
@Test
@Ignore
public void getCookiesTest() {
// 检查在Connection关闭情况下Cookie是否可以正常获取
HttpResponse res = HttpRequest.get("https://www.oschina.net/").execute();
String body = res.body();
Console.log(res.getCookies());
}
@Test
@Ignore