下面列出了java.net.CookieManager#getCookieStore ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main(String[] args) throws IOException, InterruptedException {
/*
HttpClient client = HttpClient.newBuilder()
.cookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_NONE))
.build();
System.out.println(client.cookieHandler().orElseThrow());
*/
CookieManager cm = new CookieManager();
cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
HttpClient client = HttpClient.newBuilder()
.cookieHandler(cm)
.build();
HttpRequest request = HttpRequest.newBuilder()
.header("Authorization", "Bearer Q3ku4mp_hCQGvAFeYKa0ktFCDKS3VpSA1nwf")
.uri(URI.create("https://gorest.co.in/public-api/users/1"))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("Status code: " + response.statusCode());
System.out.println("\n Body: " + response.body());
System.out.println("\nset-cookie header: " + response.headers().firstValue("set-cookie"));
CookieStore cookieStore = cm.getCookieStore();
System.out.println("\nCookies: " + cookieStore.getCookies());
}
/**
* Clears the cookies in the given cookie handler. Cookies can only be cleared if the
* cookieHandler is a CookieManager with a non-null CookieStore.
*
* @param cookieHandler the cookie handler where cookies should be cleared
* @return true if cookies were cleared; false otherwise
*/
public static boolean clearCookies(CookieHandler cookieHandler) {
if (cookieHandler instanceof CookieManager) {
CookieManager cookieManager = (CookieManager) cookieHandler;
CookieStore cookieStore = cookieManager.getCookieStore();
if (cookieStore != null) {
cookieStore.removeAll();
return true;
}
}
return false;
}
@Override public void setUp() throws Exception {
super.setUp();
server = new MockWebServer();
defaultHandler = CookieHandler.getDefault();
cookieManager = new CookieManager(createCookieStore(), null);
cookieStore = cookieManager.getCookieStore();
}
/**
* 设置头部参数
*
* @throws ProtocolException
*/
public static HttpURLConnection setConnectParam(HttpTaskOption delegate, HttpURLConnection conn) {
if (delegate.getRequestEnum() == RequestEnum.POST) {
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
}
Set<String> keys = null;
if (delegate.getHeaders() != null && delegate.getHeaders().size() > 0) {
keys = delegate.getHeaders().keySet();
for (String key : keys) {
conn.setRequestProperty(key, delegate.getHeaders().get(key));
}
}
if (conn.getRequestProperty("Accept-Language") == null) {
conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,ja;q=0.7");
}
if (conn.getRequestProperty("Accept-Encoding") == null) {
conn.setRequestProperty("Accept-Encoding", "identity");
}
if (conn.getRequestProperty("Accept-Charset") == null) {
conn.setRequestProperty("Accept-Charset", "UTF-8");
}
if (conn.getRequestProperty("Connection") == null) {
conn.setRequestProperty("Connection", "Keep-Alive");
}
if (conn.getRequestProperty("Charset") == null) {
conn.setRequestProperty("Charset", "UTF-8");
}
if (conn.getRequestProperty("User-Agent") == null) {
conn.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
}
if (conn.getRequestProperty("Accept") == null) {
StringBuilder accept = new StringBuilder();
accept.append("image/gif, ")
.append("image/jpeg, ")
.append("image/pjpeg, ")
.append("image/webp, ")
.append("image/apng, ")
.append("application/xml, ")
.append("application/xaml+xml, ")
.append("application/xhtml+xml, ")
.append("application/x-shockwave-flash, ")
.append("application/x-ms-xbap, ")
.append("application/x-ms-application, ")
.append("application/msword, ")
.append("application/vnd.ms-excel, ")
.append("application/vnd.ms-xpsdocument, ")
.append("application/vnd.ms-powerpoint, ")
.append("application/signed-exchange, ")
.append("text/plain, ")
.append("text/html, ")
.append("*/*");
conn.setRequestProperty("Accept", accept.toString());
}
//302获取重定向地址
conn.setInstanceFollowRedirects(false);
CookieManager manager = delegate.getCookieManager();
if (manager != null) {
CookieStore store = manager.getCookieStore();
if (store != null && store.getCookies().size() > 0) {
conn.setRequestProperty("Cookie",
TextUtils.join(";", store.getCookies()));
}
}
return conn;
}
/**
* {@link java.net.CookieManager#getCookieStore()}
* @since 1.6
*/
public void test_GetCookieStore() {
CookieManager cookieManager = new CookieManager(createCookieStore(), null);
CookieStore store = cookieManager.getCookieStore();
assertNotNull(store);
}