Example usage for org.apache.http.impl.cookie CookieSpecBase formatCookies

List of usage examples for org.apache.http.impl.cookie CookieSpecBase formatCookies

Introduction

In this page you can find the example usage for org.apache.http.impl.cookie CookieSpecBase formatCookies.

Prototype

List<Header> formatCookies(List<Cookie> cookies);

Source Link

Document

Create "Cookie" headers for an array of Cookies.

Usage

From source file:net.dahanne.gallery.g2.java.client.business.G2Client.java

private Header getCookieHeader(CookieSpecBase cookieSpecBase) {
    List<Cookie> cookies = new ArrayList<Cookie>();
    cookies.addAll(sessionCookies);//from   w w  w.  ja  v  a 2 s.  c om
    List<Header> cookieHeader = cookieSpecBase.formatCookies(cookies);
    return cookieHeader.get(0);
}

From source file:de.nware.app.hsDroid.provider.onlineService2Provider.java

/**
 * Stellt HTTP Anfrage und liefert deren Antwort zurck.
 * /*w  ww.  jav  a2 s  .co m*/
 * @param url
 *            die formatierte URL
 * @return die HTML/XML Antwort
 * @throws Exception
 */
private synchronized String getResponse(String url) {

    // Log.d(TAG, "URL: " + url);
    final HttpPost httpPost = new HttpPost(url);
    httpPost.addHeader("User-Agent", USER_AGENT);
    CookieSpecBase cookieSpecBase = new BrowserCompatSpec();
    List<Header> cookieHeader = cookieSpecBase.formatCookies(StaticSessionData.cookies);
    httpPost.setHeader(cookieHeader.get(0));

    // FIXME geht nicht als int Preference, wegen
    // preferenceScreen/editText...
    int connectionTimeoutMillis = Integer.valueOf(StaticSessionData.sPreferences
            .getString(getContext().getString(R.string.Preference_ConnectionTimeout), "1500"));
    HttpClient client = HttpClientFactory.getHttpClient(connectionTimeoutMillis);
    try {

        final HttpResponse response = client.execute(httpPost);

        // Prfen ob HTTP Antwort ok ist.
        final StatusLine status = response.getStatusLine();

        if (status.getStatusCode() != HttpStatus.SC_OK) {
            Log.d(TAG, "http status code: " + status.getStatusCode());
            throw new RuntimeException("Ungltige Antwort vom Server: " + status.toString());
        }

        // Hole Content Stream
        final HttpEntity entity = response.getEntity();

        // content.
        final InputStream inputStream = entity.getContent();
        final ByteArrayOutputStream content = new ByteArrayOutputStream();

        // response lesen in ByteArrayOutputStream.
        int readBytes = 0;
        while ((readBytes = inputStream.read(mContentBuffer)) != -1) {
            content.write(mContentBuffer, 0, readBytes);
        }

        // Stream nach String
        String output = new String(content.toByteArray());

        // Stream freigeben
        content.close();
        return output;

    } catch (IOException e) {
        Log.d(TAG, e.getMessage());
        throw new RuntimeException("Verbindung fehlgeschlagen: " + e.getMessage(), e);
    }

}