add additional property for httpclient v.4.5

This commit is contained in:
Loredana 2020-10-29 15:16:29 +02:00
parent 170ab9123d
commit 985d8df088
3 changed files with 19 additions and 6 deletions

View File

@ -4,12 +4,13 @@ import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.cookie.ClientCookie;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -47,6 +48,7 @@ public class HttpClientGettingCookieValueUnitTest {
BasicCookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie("custom_cookie", "test_value");
cookie.setDomain("baeldung.com");
cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "true");
cookie.setPath("/");
cookieStore.addCookie(cookie);
return cookieStore;

View File

@ -302,7 +302,7 @@
<!-- testing -->
<wiremock.version>2.5.1</wiremock.version>
<httpcore.version>4.4.11</httpcore.version>
<httpclient.version>4.5.8</httpclient.version> <!-- 4.3.6 --> <!-- 4.4-beta1 -->
<httpclient.version>4.5.8</httpclient.version>
<!-- maven plugins -->
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
</properties>

View File

@ -1,19 +1,23 @@
package com.baeldung.httpclient.sec;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.cookie.ClientCookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import com.baeldung.httpclient.ResponseUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
@ -26,6 +30,8 @@ public class HttpClientCookieLiveTest {
private CloseableHttpResponse response;
private static Logger log = LoggerFactory.getLogger(HttpClientCookieLiveTest.class);
@Before
public final void before() {
instance = HttpClientBuilder.create().build();
@ -54,11 +60,15 @@ public class HttpClientCookieLiveTest {
final BasicCookieStore cookieStore = new BasicCookieStore();
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
cookie.setDomain(".github.com");
cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "true");
cookie.setPath("/");
cookieStore.addCookie(cookie);
final HttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
final HttpGet request = new HttpGet("http://www.github.com");
DefaultHttpClient client = new DefaultHttpClient();
client.setCookieStore(cookieStore);
final HttpGet request = new HttpGet("https://www.github.com");
response = (CloseableHttpResponse) client.execute(request);
@ -70,6 +80,7 @@ public class HttpClientCookieLiveTest {
final BasicCookieStore cookieStore = new BasicCookieStore();
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
cookie.setDomain(".github.com");
cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "true");
cookie.setPath("/");
cookieStore.addCookie(cookie);
instance = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();