use of org.apache.http.client.CookieStore in project robovm by robovm.
the class ResponseProcessCookies method process.
public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
if (response == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
if (context == null) {
throw new IllegalArgumentException("HTTP context may not be null");
}
// Obtain cookie store
CookieStore cookieStore = (CookieStore) context.getAttribute(ClientContext.COOKIE_STORE);
if (cookieStore == null) {
this.log.info("Cookie store not available in HTTP context");
return;
}
// Obtain actual CookieSpec instance
CookieSpec cookieSpec = (CookieSpec) context.getAttribute(ClientContext.COOKIE_SPEC);
if (cookieSpec == null) {
this.log.info("CookieSpec not available in HTTP context");
return;
}
// Obtain actual CookieOrigin instance
CookieOrigin cookieOrigin = (CookieOrigin) context.getAttribute(ClientContext.COOKIE_ORIGIN);
if (cookieOrigin == null) {
this.log.info("CookieOrigin not available in HTTP context");
return;
}
HeaderIterator it = response.headerIterator(SM.SET_COOKIE);
processCookies(it, cookieSpec, cookieOrigin, cookieStore);
// see if the cookie spec supports cookie versioning.
if (cookieSpec.getVersion() > 0) {
// process set-cookie2 headers.
// Cookie2 will replace equivalent Cookie instances
it = response.headerIterator(SM.SET_COOKIE2);
processCookies(it, cookieSpec, cookieOrigin, cookieStore);
}
}
use of org.apache.http.client.CookieStore in project androidquery by androidquery.
the class AbstractAjaxCallback method httpDo.
private void httpDo(HttpUriRequest hr, String url, AjaxStatus status) throws ClientProtocolException, IOException {
DefaultHttpClient client = getClient();
if (proxyHandle != null) {
proxyHandle.applyProxy(this, hr, client);
}
if (AGENT != null) {
hr.addHeader("User-Agent", AGENT);
} else if (AGENT == null && GZIP) {
hr.addHeader("User-Agent", "gzip");
}
if (headers != null) {
for (String name : headers.keySet()) {
hr.addHeader(name, headers.get(name));
}
}
if (GZIP && (headers == null || !headers.containsKey("Accept-Encoding"))) {
hr.addHeader("Accept-Encoding", "gzip");
}
if (ah != null) {
ah.applyToken(this, hr);
}
String cookie = makeCookie();
if (cookie != null) {
hr.addHeader("Cookie", cookie);
}
HttpParams hp = hr.getParams();
if (proxy != null)
hp.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
if (timeout > 0) {
hp.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
hp.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
}
if (!redirect) {
hp.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
}
HttpContext context = new BasicHttpContext();
CookieStore cookieStore = new BasicCookieStore();
context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
request = hr;
if (abort) {
throw new IOException("Aborted");
}
if (SIMULATE_ERROR) {
throw new IOException("Simulated Error");
}
HttpResponse response = null;
try {
//response = client.execute(hr, context);
response = execute(hr, client, context);
} catch (HttpHostConnectException e) {
//if proxy is used, automatically retry without proxy
if (proxy != null) {
AQUtility.debug("proxy failed, retrying without proxy");
hp.setParameter(ConnRoutePNames.DEFAULT_PROXY, null);
//response = client.execute(hr, context);
response = execute(hr, client, context);
} else {
throw e;
}
}
byte[] data = null;
String redirect = url;
int code = response.getStatusLine().getStatusCode();
String message = response.getStatusLine().getReasonPhrase();
String error = null;
HttpEntity entity = response.getEntity();
File file = null;
File tempFile = null;
if (code < 200 || code >= 300) {
InputStream is = null;
try {
if (entity != null) {
is = entity.getContent();
byte[] s = toData(getEncoding(entity), is);
error = new String(s, "UTF-8");
AQUtility.debug("error", error);
}
} catch (Exception e) {
AQUtility.debug(e);
} finally {
AQUtility.close(is);
}
} else {
HttpHost currentHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
redirect = currentHost.toURI() + currentReq.getURI();
int size = Math.max(32, Math.min(1024 * 64, (int) entity.getContentLength()));
OutputStream os = null;
InputStream is = null;
try {
file = getPreFile();
if (file == null) {
os = new PredefinedBAOS(size);
} else {
//file.createNewFile();
tempFile = makeTempFile(file);
os = new BufferedOutputStream(new FileOutputStream(tempFile));
}
is = entity.getContent();
boolean gzip = "gzip".equalsIgnoreCase(getEncoding(entity));
if (gzip) {
is = new GZIPInputStream(is);
}
int contentLength = (int) entity.getContentLength();
//AQUtility.debug("gzip response", entity.getContentEncoding());
copy(is, os, contentLength, tempFile, file);
if (file == null) {
data = ((PredefinedBAOS) os).toByteArray();
} else {
if (!file.exists() || file.length() == 0) {
file = null;
}
}
} finally {
AQUtility.close(is);
AQUtility.close(os);
}
}
AQUtility.debug("response", code);
if (data != null) {
AQUtility.debug(data.length, url);
}
status.code(code).message(message).error(error).redirect(redirect).time(new Date()).data(data).file(file).client(client).context(context).headers(response.getAllHeaders());
}
use of org.apache.http.client.CookieStore in project tdi-studio-se by Talend.
the class RestClient method loginAs.
public void loginAs(String username, String password) {
try {
CookieStore cookieStore = new BasicCookieStore();
httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
String loginURL = "/loginservice";
// If you misspell a parameter you will get a HTTP 500 error
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("username", username));
urlParameters.add(new BasicNameValuePair("password", password));
urlParameters.add(new BasicNameValuePair("redirect", "false"));
// UTF-8 is mandatory otherwise you get a NPE
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(urlParameters, "utf-8");
executePostRequest(loginURL, entity);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
use of org.apache.http.client.CookieStore in project android-player-samples by BrightcoveOS.
the class MainActivity method httpGet.
public String httpGet(String url) {
String domain = getResources().getString(R.string.ais_domain);
String result = "";
CookieStore cookieStore = new BasicCookieStore();
BasicHttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
// If we have a cookie stored, parse and use it. Otherwise, use a default http client.
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
if (!authorizationCookie.equals("")) {
String[] cookies = authorizationCookie.split(";");
for (int i = 0; i < cookies.length; i++) {
String[] kvp = cookies[i].split("=");
if (kvp.length != 2) {
throw new Exception("Illegal cookie: missing key/value pair.");
}
BasicClientCookie c = new BasicClientCookie(kvp[0], kvp[1]);
c.setDomain(domain);
cookieStore.addCookie(c);
}
}
HttpResponse httpResponse = httpClient.execute(httpGet, localContext);
result = EntityUtils.toString(httpResponse.getEntity());
} catch (Exception e) {
Log.e(TAG, e.getLocalizedMessage());
}
return result;
}
use of org.apache.http.client.CookieStore in project aries by apache.
the class HttpTestCase method testSessionBean.
public void testSessionBean() throws Exception {
Bundle tb5Bundle = installBundle("tb6.jar");
try {
String path = "/foo";
RequestInfoDTO requestInfoDTO = waitFor(path);
assertEquals("foo", requestInfoDTO.servletDTO.name);
HttpClientBuilder clientBuilder = hcbf.newBuilder();
CloseableHttpClient httpclient = clientBuilder.build();
CookieStore cookieStore = new BasicCookieStore();
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
URI uri = new URIBuilder(getEndpoint()).setPath(path).setParameter("name", "test").build();
HttpGet httpget = new HttpGet(uri);
try (CloseableHttpResponse response = httpclient.execute(httpget, httpContext)) {
HttpEntity entity = response.getEntity();
assertEquals("test", read(entity));
}
for (int i = 0; i < 10; i++) {
uri = new URIBuilder(getEndpoint()).setPath(path).build();
httpget = new HttpGet(uri);
try (CloseableHttpResponse response = httpclient.execute(httpget, httpContext)) {
HttpEntity entity = response.getEntity();
assertEquals("test", read(entity));
}
}
uri = new URIBuilder(getEndpoint()).setPath(path).build();
httpget = new HttpGet(uri);
try (CloseableHttpResponse response = httpclient.execute(httpget)) {
HttpEntity entity = response.getEntity();
assertEquals("", read(entity));
}
} finally {
tb5Bundle.uninstall();
}
}
Aggregations