Search in sources :

Example 36 with HttpUrl

use of okhttp3.HttpUrl in project okhttp by square.

the class CacheTest method iteratorRemoveFromCache.

@Test
public void iteratorRemoveFromCache() throws Exception {
    // Put a response in the cache.
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("a"));
    HttpUrl url = server.url("/a");
    assertEquals("a", get(url).body().string());
    // Remove it with iteration.
    Iterator<String> i = cache.urls();
    assertEquals(url.toString(), i.next());
    i.remove();
    // Confirm that subsequent requests suffer a cache miss.
    server.enqueue(new MockResponse().setBody("b"));
    assertEquals("b", get(url).body().string());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 37 with HttpUrl

use of okhttp3.HttpUrl in project okhttp by square.

the class CacheTest method varyMultipleFieldsWithMatch.

@Test
public void varyMultipleFieldsWithMatch() throws Exception {
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Accept-Language, Accept-Charset").addHeader("Vary: Accept-Encoding").setBody("A"));
    server.enqueue(new MockResponse().setBody("B"));
    HttpUrl url = server.url("/");
    Request request = new Request.Builder().url(url).header("Accept-Language", "fr-CA").header("Accept-Charset", "UTF-8").header("Accept-Encoding", "identity").build();
    Response response1 = client.newCall(request).execute();
    assertEquals("A", response1.body().string());
    Request request1 = new Request.Builder().url(url).header("Accept-Language", "fr-CA").header("Accept-Charset", "UTF-8").header("Accept-Encoding", "identity").build();
    Response response2 = client.newCall(request1).execute();
    assertEquals("A", response2.body().string());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 38 with HttpUrl

use of okhttp3.HttpUrl in project ExoPlayer by google.

the class OkHttpDataSource method makeRequest.

/**
   * Establishes a connection.
   */
private Request makeRequest(DataSpec dataSpec) {
    long position = dataSpec.position;
    long length = dataSpec.length;
    boolean allowGzip = dataSpec.isFlagSet(DataSpec.FLAG_ALLOW_GZIP);
    HttpUrl url = HttpUrl.parse(dataSpec.uri.toString());
    Request.Builder builder = new Request.Builder().url(url);
    if (cacheControl != null) {
        builder.cacheControl(cacheControl);
    }
    if (defaultRequestProperties != null) {
        for (Map.Entry<String, String> property : defaultRequestProperties.getSnapshot().entrySet()) {
            builder.header(property.getKey(), property.getValue());
        }
    }
    for (Map.Entry<String, String> property : requestProperties.getSnapshot().entrySet()) {
        builder.header(property.getKey(), property.getValue());
    }
    if (!(position == 0 && length == C.LENGTH_UNSET)) {
        String rangeRequest = "bytes=" + position + "-";
        if (length != C.LENGTH_UNSET) {
            rangeRequest += (position + length - 1);
        }
        builder.addHeader("Range", rangeRequest);
    }
    builder.addHeader("User-Agent", userAgent);
    if (!allowGzip) {
        builder.addHeader("Accept-Encoding", "identity");
    }
    if (dataSpec.postBody != null) {
        builder.post(RequestBody.create(null, dataSpec.postBody));
    }
    return builder.build();
}
Also used : Request(okhttp3.Request) Map(java.util.Map) HttpUrl(okhttp3.HttpUrl)

Example 39 with HttpUrl

use of okhttp3.HttpUrl 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 40 with HttpUrl

use of okhttp3.HttpUrl 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)

Aggregations

HttpUrl (okhttp3.HttpUrl)72 Test (org.junit.Test)55 MockResponse (okhttp3.mockwebserver.MockResponse)53 Request (okhttp3.Request)28 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)25 IOException (java.io.IOException)20 Response (okhttp3.Response)17 MockWebServer (okhttp3.mockwebserver.MockWebServer)17 RequestBody (okhttp3.RequestBody)12 Moshi (com.squareup.moshi.Moshi)6 CookieManager (java.net.CookieManager)6 Call (retrofit2.Call)6 Callback (retrofit2.Callback)6 Response (retrofit2.Response)6 LoginFailedException (com.pokegoapi.exceptions.request.LoginFailedException)5 HttpCookie (java.net.HttpCookie)5 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 QueryRequest (zipkin.storage.QueryRequest)5 HashMap (java.util.HashMap)4