Search in sources :

Example 6 with OkHttpURLConnection

use of okhttp3.internal.huc.OkHttpURLConnection in project okhttp by square.

the class ResponseCacheTest method otherStacks_cacheHitWithoutVary.

// Other stacks (e.g. older versions of OkHttp bundled inside Android apps) can interact with the
// default ResponseCache. We try to keep this case working as much as possible because apps break
// if we don't.
@Test
public void otherStacks_cacheHitWithoutVary() throws Exception {
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("A"));
    server.enqueue(new MockResponse().setBody("FAIL"));
    // Set the cache as the shared cache.
    ResponseCache.setDefault(cache);
    // Use the platform's HTTP stack.
    URLConnection connection = server.url("/").url().openConnection();
    assertFalse(connection instanceof OkHttpURLConnection);
    assertEquals("A", readAscii(connection));
    URLConnection connection2 = server.url("/").url().openConnection();
    assertFalse(connection2 instanceof OkHttpURLConnection);
    assertEquals("A", readAscii(connection2));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 7 with OkHttpURLConnection

use of okhttp3.internal.huc.OkHttpURLConnection in project NoHttp by yanzhenjie.

the class URLConnectionFactory method open.

/**
     * Open url.
     *
     * @param url   {@link URL}.
     * @param proxy {@link Proxy}.
     * @return {@link HttpURLConnection}.
     */
public HttpURLConnection open(URL url, Proxy proxy) {
    OkHttpClient copy = mClient.newBuilder().proxy(proxy).build();
    String protocol = url.getProtocol();
    if (protocol.equals("http"))
        return new OkHttpURLConnection(url, copy);
    if (protocol.equals("https"))
        return new OkHttpsURLConnection(url, copy);
    throw new IllegalArgumentException("Unexpected protocol: " + protocol);
}
Also used : OkHttpClient(okhttp3.OkHttpClient) OkHttpsURLConnection(okhttp3.internal.huc.OkHttpsURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection)

Aggregations

HttpURLConnection (java.net.HttpURLConnection)5 Test (org.junit.Test)5 URLConnection (java.net.URLConnection)4 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)4 MockResponse (okhttp3.mockwebserver.MockResponse)4 OkHttpURLConnection (okhttp3.internal.huc.OkHttpURLConnection)3 OkHttpsURLConnection (okhttp3.internal.huc.OkHttpsURLConnection)2 IOException (java.io.IOException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 OkHttpClient (okhttp3.OkHttpClient)1