use of org.apache.http.impl.cookie.BasicClientCookie in project tutorials by eugenp.
the class HttpClientCookieLiveTest method whenSettingCookiesOnTheHttpClient_thenCookieSentCorrectly.
@Test
public final void whenSettingCookiesOnTheHttpClient_thenCookieSentCorrectly() throws IOException {
final BasicCookieStore cookieStore = new BasicCookieStore();
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
cookie.setDomain(".github.com");
cookie.setPath("/");
cookieStore.addCookie(cookie);
instance = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
final HttpGet request = new HttpGet("http://www.github.com");
response = instance.execute(request);
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}
use of org.apache.http.impl.cookie.BasicClientCookie in project tutorials by eugenp.
the class HttpClientCookieLiveTest method whenSettingCookiesOnTheRequest_thenCookieSentCorrectly.
@Test
public final void whenSettingCookiesOnTheRequest_thenCookieSentCorrectly() throws IOException {
final BasicCookieStore cookieStore = new BasicCookieStore();
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
cookie.setDomain(".github.com");
cookie.setPath("/");
cookieStore.addCookie(cookie);
instance = HttpClientBuilder.create().build();
final HttpGet request = new HttpGet("http://www.github.com");
final HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
// localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); // before 4.3
response = instance.execute(request, localContext);
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}
use of org.apache.http.impl.cookie.BasicClientCookie in project cheetah by togethwy.
the class HttpClientHelper method getDefaultClient.
public static CloseableHttpClient getDefaultClient(SiteConfig siteConfig) {
HttpClientBuilder httpClientBuilder = HttpClients.custom();
httpClientBuilder.setUserAgent(siteConfig.getUserAgent()).setRedirectStrategy(new DefaultRedirectStrategy()).setDefaultHeaders(convertMap2List(siteConfig.getHeaders()));
CookieStore cookieStore = new BasicCookieStore();
Map<String, String> cookieMap = siteConfig.getCookies();
String domain = siteConfig.getDomain();
cookieMap.forEach((name, value) -> {
BasicClientCookie cookie = new BasicClientCookie(name, value);
cookie.setDomain(domain);
cookie.setPath("/");
cookieStore.addCookie(cookie);
});
httpClientBuilder.setDefaultCookieStore(cookieStore);
return httpClientBuilder.build();
}
use of org.apache.http.impl.cookie.BasicClientCookie in project k-9 by k9mail.
the class WebDavStoreTest method createOkResponseWithCookie.
private Answer<HttpResponse> createOkResponseWithCookie() {
return new Answer<HttpResponse>() {
@Override
public HttpResponse answer(InvocationOnMock invocation) {
HttpContext context = (HttpContext) invocation.getArguments()[1];
if (context.getAttribute(ClientContext.COOKIE_STORE) != null) {
BasicCookieStore cookieStore = (BasicCookieStore) context.getAttribute(ClientContext.COOKIE_STORE);
BasicClientCookie cookie = new BasicClientCookie("cookie", "meLikeCookie");
cookieStore.addCookie(cookie);
}
return OK_200_RESPONSE;
}
};
}
use of org.apache.http.impl.cookie.BasicClientCookie in project SeaStar by 13120241790.
the class SyncHttpClient method sendRequest.
/**
* Puts a new request in queue as a new thread in pool to be executed
*
* @param client
* HttpClient to be used for request, can differ in single requests
* @param httpContext
* HttpContext in which the request will be executed
* @param uriRequest
* instance of HttpUriRequest, which means it must be of
* HttpDelete, HttpPost, HttpGet, HttpPut, etc.
* @param contentType
* MIME body type, for POST and PUT requests, may be null
* @param context
* Context of Android application
* @return
* @throws HttpException
*/
protected String sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, Context context) throws HttpException {
String responseBody = "";
if (contentType != null) {
uriRequest.addHeader("Content-Type", contentType);
}
// set cookie
List<Cookie> list = cookieStore.getCookies();
if (list != null && list.size() > 0) {
for (Cookie cookie : list) {
uriRequest.setHeader("Cookie", cookie.getValue());
}
}
try {
// get the response from assets
URI uri = uriRequest.getURI();
NLog.e(tag, "url : " + uri.toString());
String scheme = uri.getScheme();
if (!TextUtils.isEmpty(scheme) && ASSETS_PATH.equals(scheme)) {
String fileName = uri.getAuthority();
InputStream intput = context.getAssets().open(fileName);
responseBody = inputSteamToString(intput);
NLog.e(tag, "responseBody : " + responseBody);
return responseBody;
}
// get the response from network
HttpEntity bufferEntity = null;
HttpResponse response = client.execute(uriRequest, httpContext);
HttpEntity entity = response.getEntity();
if (entity != null) {
bufferEntity = new BufferedHttpEntity(entity);
responseBody = EntityUtils.toString(bufferEntity, ENCODE_UTF8);
// if(NLog.isDebug()){
// String key = uriRequest.getURI().toString().replace("/", "");
// CacheManager.saveTestData(responseBody, key);
// }
NLog.e(tag, "responseBody : " + responseBody);
}
// get cookie to save local
Header[] headers = response.getHeaders("Set-Cookie");
if (headers != null && headers.length > 0) {
for (int i = 0; i < headers.length; i++) {
String cookie = headers[i].getValue();
BasicClientCookie newCookie = new BasicClientCookie("cookie" + i, cookie);
cookieStore.addCookie(newCookie);
}
}
} catch (Exception e) {
e.printStackTrace();
throw new HttpException(e);
}
return responseBody;
}
Aggregations