Hey all I am trying my best to convert this working CURL script to Java:
@ECHO off
set COOKIES=.\cookies.txt
set USER=myUN
set PASSWORD="myPW"
set HOST=https://zzzzzzz.zzzzz.zz.zz:9443
cls
ECHO "Get the session cookie-------------------------------------"
set out1 = curl -k -c %cookies% "%host%/qm/authenticated/identity" > nul
ECHO "-----------------------------------------------------------"
ECHO "Use the session cookie to logon----------------------------"
curl -k -L -b %COOKIES% -c %COOKIES% -d j_username=%USER% -d j_password=%PASSWORD% %host%/qm/authenticated/j_security_check > nul
ECHO "-----------------------------------------------------------"
ECHO "Use the cookie to get the catalog--------------------------"
curl -k -L -b %COOKIES% -H "Accept: application/xml" %host%/qm/process/project-areas/_zzzzzzzzzzzzzzzzzzzz/members
ECHO "-----------------------------------------------------------"
where:
-k: Allow insecure server connections when using SSL
-c <filename>: Write cookies to <filename> after operation
-L: Follow redirects
-b <data>: Send cookies from string/file
-H <header/@file>: Pass custom header(s) to server
-d <data>: HTTP POST data
I've searched around for java code that includes an example of creating a cookie but I have yet to find something that would help me with my code above.
The closest I could find and modify the POST call is this:
CookieStore cookieStore = new BasicCookieStore();
String USER = "myUN";
String PASSWORD = "myPW";
String HOST = "https://zzzzzzz.zzzzz.zz.zz:9443";
// CookieSpecs.STANDARD is the RFC 6265 compliant policy
RequestConfig requestConfig = RequestConfig
.custom()
.setCookieSpec(CookieSpecs.STANDARD)
.build();
// automatically follow redirects
CloseableHttpClient client = HttpClients
.custom()
.setRedirectStrategy(new LaxRedirectStrategy())
.setDefaultRequestConfig(requestConfig)
.setDefaultCookieStore(cookieStore)
.build();
HttpPost postIT = new HttpPost(HOST + "/qm/authenticated/identity");
List<NameValuePair> urlParams = new ArrayList<>();
urlParams.add(new BasicNameValuePair("j_username", USER));
urlParams.add(new BasicNameValuePair("j_password", PASSWORD));
postIT.setEntity(new UrlEncodedFormEntity(urlParams));
HttpResponse mmmCookie = client.execute(postIT);
// ... we have our cookie!
For postIT the value is:
POST https://zzzzz.zzz.zzzz.zzzz:9443/qm/authenticated/identity HTTP/1.1
However, it has an error of:
error: null
Not sure why the error is null since postIT has data? So not only am I not able to run the modified code I did, I'm still wondering how I go about calling a GET command using the cookie even if the code above worked.
So, in a nutshell:
- Fix httpPost error.
- How to send cookie on other GET methods.
Help would be great!
update for VGR:
public static void main(String[] args) throws IOException {
Path currentRelativePath = Paths.get("").toAbsolutePath();
PermissiveTrustManager blah = new PermissiveTrustManager();
blah.readMembers("https://zzzzzz.zzzz.zz.zzz:9443", "zzzzzz", "zzzzzzz", currentRelativePath);
}
The error is on this line:
check(tm -> tm.checkServerTrusted(certChain, authType, socket), socket);
certChain, authType and socket all have data in them.
error: nullcome from? How did you print or view it?error: nullto appear on the screen somewhere?