Search in sources :

Example 71 with CookieManager

use of java.net.CookieManager in project remusic by aa112901.

the class HttpUtil method postUrl.

public static void postUrl(Context context, String j) {
    try {
        String action = "https://music.163.com/weapi/login/";
        RequestBody formBody = new FormEncodingBuilder().build();
        Log.e("post", "p");
        Request request = new Request.Builder().url(action).header("Content-Type", "application/x-www-form-urlencoded").header("Host", "music.163.com").header("Cookie", "appver=1.5.0.75771").header("Referer", "http://music.163.com/").header("Connection", "keep-alive").header("Accept-Encoding", "gzip,deflate").header("Accept", "*/*").header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36").post(formBody).build();
        mOkHttpClient.setCookieHandler(new CookieManager(new PersistentCookieStore(context.getApplicationContext()), CookiePolicy.ACCEPT_ALL));
        Response response = mOkHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            Log.e("respose", response.body().string());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Response(com.squareup.okhttp.Response) Request(com.squareup.okhttp.Request) FormEncodingBuilder(com.squareup.okhttp.FormEncodingBuilder) CookieManager(java.net.CookieManager) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RequestBody(com.squareup.okhttp.RequestBody)

Example 72 with CookieManager

use of java.net.CookieManager in project remusic by aa112901.

the class HttpUtil method postNetease.

public static void postNetease(Context context, String j) {
    try {
        String action = "https://music.163.com/weapi/login/";
        RequestBody formBody = new FormEncodingBuilder().add("params", "9NdyZTlp0Q/f1E1ora4tGM0uLYXqh7MD0mk7632ilWQvRDPZ02UkHrGFUccwW4HZYpacpPnmE+oMr/HI/vhuQvg8zYKgDP6NOaXG8nKDJpQTfOAiXT5KDrJOvb7ejSj/").add("encSeckey", "ae878167c394a959699c025a5c36043d0ae043c42d7f55fe4d1191c8ac9f3abe285b78c4a25ed6d9394a0ba0cb83a9a62de697199bd337f1de183bb07d6764a051495ea873ad615bb0a7e69f44d9168fc78ed1d61feb142ad06679dce58257ee9005756a18032ff499a4e24f7658bb59de2219f21f568301d43dba500e0c2d3b").build();
        String json = "{\"params\": \"9NdyZTlp0Q/f1E1ora4tGM0uLYXqh7MD0mk7632ilWQvRDPZ02UkHrGFUccwW4HZYpacpPnmE+oMr/HI/vhuQvg8zYKgDP6NOaXG8nKDJpQTfOAiXT5KDrJOvb7ejSj/\",  " + "\"encSecKey\": \"ae878167c394a959699c025a5c36043d0ae043c42d7f55fe4d1191c8ac9f3abe285b78c4a25ed6d9394a0ba0cb83a9a62de697199bd337f1de183bb07d6764a051495ea873ad615bb0a7e69f44d9168fc78ed1d61feb142ad06679dce58257ee9005756a18032ff499a4e24f7658bb59de2219f21f568301d43dba500e0c2d3b\"}";
        RequestBody requestBody = RequestBody.create(MediaType.parse("JSON"), json);
        Log.e("post", "p");
        Request request = new Request.Builder().url(action).header("Content-Type", "application/x-www-form-urlencoded").header("Host", "music.163.com").header("Cookie", "appver=1.5.0.75771").header("Referer", "http://music.163.com/").header("Connection", "keep-alive").header("Accept-Encoding", "gzip,deflate").header("Accept", "*/*").header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36").post(requestBody).build();
        mOkHttpClient.setCookieHandler(new CookieManager(new PersistentCookieStore(context.getApplicationContext()), CookiePolicy.ACCEPT_ALL));
        Response response = mOkHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            Log.e("respose", response.body().string());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Response(com.squareup.okhttp.Response) Request(com.squareup.okhttp.Request) FormEncodingBuilder(com.squareup.okhttp.FormEncodingBuilder) CookieManager(java.net.CookieManager) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RequestBody(com.squareup.okhttp.RequestBody)

Example 73 with CookieManager

use of java.net.CookieManager in project okhttp by square.

the class ResponseCacheTest method setUp.

@Before
public void setUp() throws Exception {
    server.setProtocolNegotiationEnabled(false);
    cache = AndroidShimResponseCache.create(cacheRule.getRoot(), 10 * 1024 * 1024);
    urlFactory = new OkUrlFactory(new OkHttpClient());
    AndroidInternal.setResponseCache(urlFactory, cache);
    cookieManager = new CookieManager();
}
Also used : OkUrlFactory(okhttp3.OkUrlFactory) OkHttpClient(okhttp3.OkHttpClient) CookieManager(java.net.CookieManager) Before(org.junit.Before)

Example 74 with CookieManager

use of java.net.CookieManager in project okhttp by square.

the class CookiesTest method acceptOriginalServerMatchesExactly.

@Test
public void acceptOriginalServerMatchesExactly() throws Exception {
    CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
    JavaNetCookieJar cookieJar = new JavaNetCookieJar(cookieManager);
    HttpUrl url = HttpUrl.parse("https://squareup.com/");
    cookieJar.saveFromResponse(url, Arrays.asList(Cookie.parse(url, "a=android; Domain=squareup.com")));
    List<Cookie> actualCookies = cookieJar.loadForRequest(url);
    assertEquals(1, actualCookies.size());
    assertEquals("a", actualCookies.get(0).name());
    assertEquals("android", actualCookies.get(0).value());
}
Also used : HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Test(org.junit.Test)

Example 75 with CookieManager

use of java.net.CookieManager in project okhttp by square.

the class CookiesTest method testCookiesSentIgnoresCase.

@Test
public void testCookiesSentIgnoresCase() throws Exception {
    client = client.newBuilder().cookieJar(new JavaNetCookieJar(new CookieManager() {

        @Override
        public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) throws IOException {
            Map<String, List<String>> result = new LinkedHashMap<>();
            result.put("COOKIE", Collections.singletonList("Bar=bar"));
            result.put("cooKIE2", Collections.singletonList("Baz=baz"));
            return result;
        }
    })).build();
    MockWebServer server = new MockWebServer();
    server.enqueue(new MockResponse());
    server.start();
    get(server.url("/"));
    RecordedRequest request = server.takeRequest();
    assertEquals("Bar=bar; Baz=baz", request.getHeader("Cookie"));
    assertNull(request.getHeader("Cookie2"));
    assertNull(request.getHeader("Quux"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) List(java.util.List) URI(java.net.URI) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) CookieManager(java.net.CookieManager) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

CookieManager (java.net.CookieManager)94 HttpCookie (java.net.HttpCookie)53 URI (java.net.URI)50 CookieStore (java.net.CookieStore)28 Test (org.junit.Test)16 MockResponse (com.google.mockwebserver.MockResponse)14 MockWebServer (com.google.mockwebserver.MockWebServer)14 List (java.util.List)9 MockResponse (okhttp3.mockwebserver.MockResponse)9 ArrayList (java.util.ArrayList)7 MockWebServer (okhttp3.mockwebserver.MockWebServer)7 RecordedRequest (com.google.mockwebserver.RecordedRequest)6 IOException (java.io.IOException)6 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Project (com.kickstarter.models.Project)3 Headers (com.koushikdutta.async.http.Headers)3 OkHttpClient (okhttp3.OkHttpClient)3 RefTag (com.kickstarter.libs.RefTag)2