Search in sources :

Example 1 with CookieHandler

use of java.net.CookieHandler in project j2objc by google.

the class IosHttpURLConnection method loadRequestCookies.

/**
   * Add any cookies for this URI to the request headers.
   */
private void loadRequestCookies() throws IOException {
    CookieHandler cookieHandler = CookieHandler.getDefault();
    if (cookieHandler != null) {
        try {
            URI uri = getURL().toURI();
            Map<String, List<String>> cookieHeaders = cookieHandler.get(uri, getHeaderFieldsDoNotForceResponse());
            for (Map.Entry<String, List<String>> entry : cookieHeaders.entrySet()) {
                String key = entry.getKey();
                if (("Cookie".equalsIgnoreCase(key) || "Cookie2".equalsIgnoreCase(key)) && !entry.getValue().isEmpty()) {
                    List<String> cookies = entry.getValue();
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0, size = cookies.size(); i < size; i++) {
                        if (i > 0) {
                            sb.append("; ");
                        }
                        sb.append(cookies.get(i));
                    }
                    setHeader(key, sb.toString());
                }
            }
        } catch (URISyntaxException e) {
            throw new IOException(e);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) CookieHandler(java.net.CookieHandler)

Example 2 with CookieHandler

use of java.net.CookieHandler in project XobotOS by xamarin.

the class HttpEngine method prepareRawRequestHeaders.

/**
     * Populates requestHeaders with defaults and cookies.
     *
     * <p>This client doesn't specify a default {@code Accept} header because it
     * doesn't know what content types the application is interested in.
     */
private void prepareRawRequestHeaders() throws IOException {
    requestHeaders.getHeaders().setStatusLine(getRequestLine());
    if (requestHeaders.getUserAgent() == null) {
        requestHeaders.setUserAgent(getDefaultUserAgent());
    }
    if (requestHeaders.getHost() == null) {
        requestHeaders.setHost(getOriginAddress(policy.getURL()));
    }
    if (httpMinorVersion > 0 && requestHeaders.getConnection() == null) {
        requestHeaders.setConnection("Keep-Alive");
    }
    if (requestHeaders.getAcceptEncoding() == null) {
        transparentGzip = true;
        requestHeaders.setAcceptEncoding("gzip");
    }
    if (hasRequestBody() && requestHeaders.getContentType() == null) {
        requestHeaders.setContentType("application/x-www-form-urlencoded");
    }
    long ifModifiedSince = policy.getIfModifiedSince();
    if (ifModifiedSince != 0) {
        requestHeaders.setIfModifiedSince(new Date(ifModifiedSince));
    }
    CookieHandler cookieHandler = CookieHandler.getDefault();
    if (cookieHandler != null) {
        requestHeaders.addCookies(cookieHandler.get(uri, requestHeaders.getHeaders().toMultimap()));
    }
}
Also used : Date(java.util.Date) CookieHandler(java.net.CookieHandler)

Example 3 with CookieHandler

use of java.net.CookieHandler in project intellij-plugins by StepicOrg.

the class AuthDialog method initCookieManager.

@NotNull
private CookieManager initCookieManager(boolean clearCookies) {
    CookieHandler cookieManager = CookieManager.getDefault();
    if (!(cookieManager instanceof CookieManager)) {
        cookieManager = new CookieManager();
    }
    if (clearCookies) {
        ((CookieManager) cookieManager).clear();
        clearedCookies = true;
    }
    CookieManager.setDefault(cookieManager);
    return (CookieManager) cookieManager;
}
Also used : CookieManager(org.stepik.plugin.auth.webkit.network.CookieManager) CookieHandler(java.net.CookieHandler) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with CookieHandler

use of java.net.CookieHandler in project QuickAndroid by ImKarl.

the class OkHttpCacheHelper method networkRequest.

private static Request networkRequest(OkHttpClient client, Request request) throws IOException {
    Request.Builder result = request.newBuilder();
    if (request.header("Host") == null) {
        result.header("Host", Util.hostHeader(request.httpUrl()));
    }
    if (request.header("Connection") == null) {
        result.header("Connection", "Keep-Alive");
    }
    if (request.header("Accept-Encoding") == null) {
        result.header("Accept-Encoding", "gzip");
    }
    CookieHandler cookieHandler = client.getCookieHandler();
    if (cookieHandler != null) {
        // Capture the request headers added so far so that they can be offered to the CookieHandler.
        // This is mostly to stay close to the RI; it is unlikely any of the headers above would
        // affect cookie choice besides "Host".
        Map<String, List<String>> headers = OkHeaders.toMultimap(result.build().headers(), null);
        Map<String, List<String>> cookies = cookieHandler.get(request.uri(), headers);
        // Add any new cookies to the request.
        OkHeaders.addCookies(result, cookies);
    }
    if (request.header("User-Agent") == null) {
        result.header("User-Agent", Version.userAgent());
    }
    return result.build();
}
Also used : Request(com.squareup.okhttp.Request) List(java.util.List) CookieHandler(java.net.CookieHandler)

Example 5 with CookieHandler

use of java.net.CookieHandler in project j2objc by google.

the class IosHttpURLConnection method saveResponseCookies.

/**
   * Store any returned cookies.
   */
private void saveResponseCookies() throws IOException {
    CookieHandler cookieHandler = CookieHandler.getDefault();
    if (cookieHandler != null) {
        try {
            URI uri = getURL().toURI();
            cookieHandler.put(uri, getHeaderFieldsDoNotForceResponse());
        } catch (URISyntaxException e) {
            throw new IOException(e);
        }
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) CookieHandler(java.net.CookieHandler)

Aggregations

CookieHandler (java.net.CookieHandler)13 URI (java.net.URI)4 Date (java.util.Date)4 IOException (java.io.IOException)3 List (java.util.List)3 URISyntaxException (java.net.URISyntaxException)2 Map (java.util.Map)2 KeyEvent (android.view.KeyEvent)1 SurfaceView (android.view.SurfaceView)1 View (android.view.View)1 OnKeyListener (android.view.View.OnKeyListener)1 FrameLayout (android.widget.FrameLayout)1 AspectRatioFrameLayout (com.google.android.exoplayer.AspectRatioFrameLayout)1 AudioCapabilitiesReceiver (com.google.android.exoplayer.audio.AudioCapabilitiesReceiver)1 Request (com.squareup.okhttp.Request)1 HttpServer (com.sun.net.httpserver.HttpServer)1 InetSocketAddress (java.net.InetSocketAddress)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 ArrayList (java.util.ArrayList)1