Java Code Examples for java.net.CookieManager#getCookieStore()
The following examples show how to use
java.net.CookieManager#getCookieStore() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Main.java From Java-Coding-Problems with MIT License | 5 votes |
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()); }
Example 2
Source File: GingerbreadUtil.java From appinventor-extensions with Apache License 2.0 | 5 votes |
/** * 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; }
Example 3
Source File: AbstractCookiesTest.java From j2objc with Apache License 2.0 | 5 votes |
@Override public void setUp() throws Exception { super.setUp(); server = new MockWebServer(); defaultHandler = CookieHandler.getDefault(); cookieManager = new CookieManager(createCookieStore(), null); cookieStore = cookieManager.getCookieStore(); }
Example 4
Source File: ConnectionHelp.java From Aria with Apache License 2.0 | 4 votes |
/** * 设置头部参数 * * @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; }
Example 5
Source File: AbstractCookiesTest.java From j2objc with Apache License 2.0 | 4 votes |
/** * {@link java.net.CookieManager#getCookieStore()} * @since 1.6 */ public void test_GetCookieStore() { CookieManager cookieManager = new CookieManager(createCookieStore(), null); CookieStore store = cookieManager.getCookieStore(); assertNotNull(store); }