Search in sources :

Example 41 with Cookie

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

the class PersistentCookieStore method decodeCookie.

/**
     * 将字符串反序列化成cookies
     *
     * @param cookieString cookies string
     * @return cookie object
     */
private Cookie decodeCookie(String cookieString) {
    byte[] bytes = hexStringToByteArray(cookieString);
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    Cookie cookie = null;
    try {
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
        cookie = ((SerializableHttpCookie) objectInputStream.readObject()).getCookie();
    } catch (IOException e) {
        Log.d(LOG_TAG, "IOException in decodeCookie", e);
    } catch (ClassNotFoundException e) {
        Log.d(LOG_TAG, "ClassNotFoundException in decodeCookie", e);
    }
    return cookie;
}
Also used : Cookie(okhttp3.Cookie) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 42 with Cookie

use of okhttp3.Cookie in project okhttputils by hongyangAndroid.

the class PersistentCookieStore method decodeCookie.

protected Cookie decodeCookie(String cookieString) {
    byte[] bytes = hexStringToByteArray(cookieString);
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    Cookie cookie = null;
    try {
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
        cookie = ((SerializableHttpCookie) objectInputStream.readObject()).getCookie();
    } catch (IOException e) {
        Log.d(LOG_TAG, "IOException in decodeCookie", e);
    } catch (ClassNotFoundException e) {
        Log.d(LOG_TAG, "ClassNotFoundException in decodeCookie", e);
    }
    return cookie;
}
Also used : Cookie(okhttp3.Cookie) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 43 with Cookie

use of okhttp3.Cookie in project keywhiz by square.

the class CommandExecutor method executeCommand.

public void executeCommand() throws IOException {
    if (command == null) {
        if (config.version) {
            System.out.println("Version: " + APP_VERSION);
        } else {
            System.err.println("Must specify a command.");
            parentCommander.usage();
        }
        return;
    }
    HttpUrl url;
    if (Strings.isNullOrEmpty(config.url)) {
        url = HttpUrl.parse("https://localhost:4444");
        System.out.println("Server URL not specified (--url flag), assuming " + url);
    } else {
        url = HttpUrl.parse(config.url);
        if (url == null) {
            System.err.print("Invalid URL " + config.url);
            return;
        }
    }
    KeywhizClient client;
    OkHttpClient httpClient;
    String user = config.getUser().orElse(USER_NAME.value());
    Path cookiePath = cookieDir.resolve(format(".keywhiz.%s.cookie", user));
    try {
        List<HttpCookie> cookieList = ClientUtils.loadCookies(cookiePath);
        httpClient = ClientUtils.sslOkHttpClient(config.getDevTrustStore(), cookieList);
        client = new KeywhizClient(mapper, httpClient, url);
        // Try a simple get request to determine whether or not the cookies are still valid
        if (!client.isLoggedIn()) {
            throw new UnauthorizedException();
        }
    } catch (IOException e) {
        // Either could not find the cookie file, or the cookies were expired -- must login manually.
        httpClient = ClientUtils.sslOkHttpClient(config.getDevTrustStore(), ImmutableList.of());
        client = new KeywhizClient(mapper, httpClient, url);
        char[] password = ClientUtils.readPassword(user);
        client.login(user, password);
        Arrays.fill(password, '\0');
    }
    // Save/update the cookies if we logged in successfully
    ClientUtils.saveCookies(cookiePath);
    Printing printing = new Printing(client);
    Command cmd = Command.valueOf(command.toUpperCase().trim());
    switch(cmd) {
        case LIST:
            new ListAction((ListActionConfig) commands.get(command), client, printing).run();
            break;
        case DESCRIBE:
            new DescribeAction((DescribeActionConfig) commands.get(command), client, printing).run();
            break;
        case ADD:
            new AddAction((AddActionConfig) commands.get(command), client, mapper).run();
            break;
        case UPDATE:
            new UpdateAction((UpdateActionConfig) commands.get(command), client, mapper).run();
            break;
        case DELETE:
            new DeleteAction((DeleteActionConfig) commands.get(command), client).run();
            break;
        case ASSIGN:
            new AssignAction((AssignActionConfig) commands.get(command), client).run();
            break;
        case UNASSIGN:
            new UnassignAction((UnassignActionConfig) commands.get(command), client).run();
            break;
        case VERSIONS:
            new ListVersionsAction((ListVersionsActionConfig) commands.get(command), client, printing).run();
            break;
        case ROLLBACK:
            new RollbackAction((RollbackActionConfig) commands.get(command), client).run();
            break;
        case LOGIN:
            // User is already logged in at this point
            break;
        default:
            commander.usage();
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) UnauthorizedException(keywhiz.client.KeywhizClient.UnauthorizedException) Path(java.nio.file.Path) KeywhizClient(keywhiz.client.KeywhizClient) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) HttpCookie(java.net.HttpCookie)

Example 44 with Cookie

use of okhttp3.Cookie in project okhttp by square.

the class UrlConnectionCacheTest method cachePlusCookies.

@Test
public void cachePlusCookies() throws Exception {
    RecordingCookieJar cookieJar = new RecordingCookieJar();
    urlFactory.setClient(urlFactory.client().newBuilder().cookieJar(cookieJar).build());
    server.enqueue(new MockResponse().addHeader("Set-Cookie: a=FIRST").addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Cache-Control: max-age=0").setBody("A"));
    server.enqueue(new MockResponse().addHeader("Set-Cookie: a=SECOND").setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    URL url = server.url("/").url();
    assertEquals("A", readAscii(urlFactory.open(url)));
    cookieJar.assertResponseCookies("a=FIRST; path=/");
    assertEquals("A", readAscii(urlFactory.open(url)));
    cookieJar.assertResponseCookies("a=SECOND; path=/");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) URL(java.net.URL) Test(org.junit.Test)

Example 45 with Cookie

use of okhttp3.Cookie in project okhttp by square.

the class CacheTest method cachePlusCookies.

@Test
public void cachePlusCookies() throws Exception {
    RecordingCookieJar cookieJar = new RecordingCookieJar();
    client = client.newBuilder().cookieJar(cookieJar).build();
    server.enqueue(new MockResponse().addHeader("Set-Cookie: a=FIRST").addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Cache-Control: max-age=0").setBody("A"));
    server.enqueue(new MockResponse().addHeader("Set-Cookie: a=SECOND").setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    HttpUrl url = server.url("/");
    assertEquals("A", get(url).body().string());
    cookieJar.assertResponseCookies("a=FIRST; path=/");
    assertEquals("A", get(url).body().string());
    cookieJar.assertResponseCookies("a=SECOND; path=/");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) 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