Http 401 Unauthorized even after authentication

Guys  Please help me 

I am trying access the getDomains API in Performance Center.

I am able to authenticate to  "LoadTest/rest/authentication-point/authenticate" and get The Cookies 

but when i set the cookies and try to access the "/LoadTest/rest/domains" i get the Http 401 Unauthorized error

 

Here is the code :

 


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Base64;
import java.util.List;


import okhttp3.Cookie;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ZipDownload {

public static void main(String[] args) throws IOException, URISyntaxException {
// TODO Auto-generated method stub

 

CookieManager customCookieManager = new CookieManager();
customCookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(customCookieManager);
String authorization = "XXXXXFFXFXXX";

String encoding = Base64.getEncoder().encodeToString(authorization.getBytes("UTF-8"));

System.out.println(encoding);
authorization = "basic " encoding;
System.out.println(authorization);



OkHttpClient client = new OkHttpClient().newBuilder().build();

HttpUrl httpurl = new HttpUrl.Builder()
.scheme("http")
.host("Server Name") .addPathSegment("LoadTest")
.addPathSegment("rest") .addPathSegment("authentication-point")
.addPathSegment("authenticate").build();

System.out.println(httpurl.toString());
Request request = new Request.Builder()
.url(httpurl) .header("Authorization", authorization).build();
System.out.println("coming till here");
Response response = client.newCall(request).execute();
System.out.println(response.code());
System.out.println(response.headers());
System.out.println("RESPONSE:==:=:=:=\n" response);
System.out.println();
CookieStore cookieJar =customCookieManager.getCookieStore();

List<HttpCookie> cookJar=cookieJar.getCookies();
System.out.println("tried code ");
System.out.println(cookJar.size());

for(HttpCookie cookie:cookJar) {
System.out.println("entered for " cookJar.size());
System.out.println("from cookiehandler:==" cookie);
}

String LWSSO_COOKIE_KEY= null;
String QCSession= null;
int count = 1;
List<Cookie> cookies = Cookie.parseAll(httpurl, response.headers());
StringBuilder LWSSSO_Cookieheader = new StringBuilder();
StringBuilder QCSession_Cookieheader = new StringBuilder();




for(Cookie cookie: cookies) { if(count == 1) { 
LWSSSO_Cookieheader.append(cookie.name()).append("=").append(cookie.value());

} else if(count ==2) { 
QCSession_Cookieheader.append(cookie.name()).append("=").append(cookie.value());
}
count ; }


System.out.println(LWSSSO_Cookieheader.toString().substring(0,
LWSSSO_Cookieheader.toString().length()));
System.out.println(QCSession_Cookieheader.toString().substring(0,
QCSession_Cookieheader.toString().length()));




URL url1 = new URL("http://SERVER Name/LoadTest/rest/domains");
for(HttpCookie cookie:cookJar) {
cookieJar.add(url1.toURI(),cookie);

}


connection.setRequestProperty("Cookie","LWSSO_COOKIE_KEY={" LWSSSO_Cookieheader.toString() "};");
connection.addRequestProperty("Cookie","QCSession={" QCSession_Cookieheader.toString() "};");


connection.setRequestMethod("GET");

InputStream in1 = connection.getInputStream();
System.out.println("IN:=:=" in1.read());

}

public static void copy(InputStream input, OutputStream output, int bufferSize) throws IOException {

byte[] buf = new byte[bufferSize];
int n = input.read(buf);
while (n >= 0) {
output.write(buf, 0, n);
n = input.read(buf);
}

output.flush();

}

public static Cookie createNonPersistentCookie() {
return new Cookie.Builder().domain(Server Name).path("/").name("cookie-name")
.value("cookie-value").httpOnly().secure().build();

}

}

We tried all the possible ways incluing cookie store . It is working fine when run through postman and curl.

Please help as soon as possible.