Search in sources :

Example 1 with Cookie

use of okhttp3.Cookie in project okhttp-OkGo by jeasonlzy.

the class CookieActivity method removeCookie.

@OnClick(R.id.removeCookie)
public void removeCookie(View view) {
    HttpUrl httpUrl = HttpUrl.parse(Urls.URL_METHOD);
    CookieStore cookieStore = OkGo.getInstance().getCookieJar().getCookieStore();
    cookieStore.removeCookie(httpUrl);
    showToast("详细移除cookie的代码,请看demo的代码");
}
Also used : CookieStore(com.lzy.okgo.cookie.store.CookieStore) HttpUrl(okhttp3.HttpUrl) OnClick(butterknife.OnClick)

Example 2 with Cookie

use of okhttp3.Cookie in project okhttp-OkGo by jeasonlzy.

the class CookieActivity method getAllCookie.

@OnClick(R.id.getAllCookie)
public void getAllCookie(View view) {
    //一般手动取出cookie的目的只是交给 webview 等等,非必要情况不要自己操作
    CookieStore cookieStore = OkGo.getInstance().getCookieJar().getCookieStore();
    List<Cookie> allCookie = cookieStore.getAllCookie();
    showToast("所有cookie如下:" + allCookie.toString());
}
Also used : Cookie(okhttp3.Cookie) CookieStore(com.lzy.okgo.cookie.store.CookieStore) OnClick(butterknife.OnClick)

Example 3 with Cookie

use of okhttp3.Cookie in project okhttp-OkGo by jeasonlzy.

the class CookieActivity method getCookie.

@OnClick(R.id.getCookie)
public void getCookie(View view) {
    //一般手动取出cookie的目的只是交给 webview 等等,非必要情况不要自己操作
    CookieStore cookieStore = OkGo.getInstance().getCookieJar().getCookieStore();
    HttpUrl httpUrl = HttpUrl.parse(Urls.URL_METHOD);
    List<Cookie> cookies = cookieStore.getCookie(httpUrl);
    showToast(httpUrl.host() + "对应的cookie如下:" + cookies.toString());
}
Also used : Cookie(okhttp3.Cookie) CookieStore(com.lzy.okgo.cookie.store.CookieStore) HttpUrl(okhttp3.HttpUrl) OnClick(butterknife.OnClick)

Example 4 with Cookie

use of okhttp3.Cookie in project okhttp by square.

the class CookiesTest method testNetscapeResponse.

@Test
public void testNetscapeResponse() throws Exception {
    CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
    client = client.newBuilder().cookieJar(new JavaNetCookieJar(cookieManager)).build();
    MockWebServer server = new MockWebServer();
    server.start();
    HttpUrl urlWithIpAddress = urlWithIpAddress(server, "/path/foo");
    server.enqueue(new MockResponse().addHeader("Set-Cookie: a=android; " + "expires=Fri, 31-Dec-9999 23:59:59 GMT; " + "path=/path; " + "domain=" + urlWithIpAddress.host() + "; " + "secure"));
    get(urlWithIpAddress);
    List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
    assertEquals(1, cookies.size());
    HttpCookie cookie = cookies.get(0);
    assertEquals("a", cookie.getName());
    assertEquals("android", cookie.getValue());
    assertEquals(null, cookie.getComment());
    assertEquals(null, cookie.getCommentURL());
    assertEquals(false, cookie.getDiscard());
    assertTrue(cookie.getMaxAge() > 100000000000L);
    assertEquals("/path", cookie.getPath());
    assertEquals(true, cookie.getSecure());
    assertEquals(0, cookie.getVersion());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Test(org.junit.Test)

Example 5 with Cookie

use of okhttp3.Cookie in project okhttp by square.

the class CookiesTest method testRfc2109Response.

@Test
public void testRfc2109Response() throws Exception {
    CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
    client = client.newBuilder().cookieJar(new JavaNetCookieJar(cookieManager)).build();
    MockWebServer server = new MockWebServer();
    server.start();
    HttpUrl urlWithIpAddress = urlWithIpAddress(server, "/path/foo");
    server.enqueue(new MockResponse().addHeader("Set-Cookie: a=android; " + "Comment=this cookie is delicious; " + "Domain=" + urlWithIpAddress.host() + "; " + "Max-Age=60; " + "Path=/path; " + "Secure; " + "Version=1"));
    get(urlWithIpAddress);
    List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
    assertEquals(1, cookies.size());
    HttpCookie cookie = cookies.get(0);
    assertEquals("a", cookie.getName());
    assertEquals("android", cookie.getValue());
    assertEquals(null, cookie.getCommentURL());
    assertEquals(false, cookie.getDiscard());
    // Converting to a fixed date can cause rounding!
    assertEquals(60.0, cookie.getMaxAge(), 1.0);
    assertEquals("/path", cookie.getPath());
    assertEquals(true, cookie.getSecure());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Test(org.junit.Test)

Aggregations

Response (okhttp3.Response)34 IOException (java.io.IOException)33 Request (okhttp3.Request)33 Call (okhttp3.Call)25 Callback (okhttp3.Callback)22 RequestBody (okhttp3.RequestBody)21 Test (org.junit.Test)18 Cookie (okhttp3.Cookie)17 MockResponse (okhttp3.mockwebserver.MockResponse)16 FormBody (okhttp3.FormBody)12 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)10 CookieManager (java.net.CookieManager)9 HttpCookie (java.net.HttpCookie)9 MockWebServer (okhttp3.mockwebserver.MockWebServer)8 OkHttpClient (okhttp3.OkHttpClient)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 HttpUrl (okhttp3.HttpUrl)5 SharedPreferences (android.content.SharedPreferences)4 OnClick (butterknife.OnClick)4