I have tried multiple things to correct this and not sure what the issue is. For the POST request I have tried encoding the username credentials in Base64 and and set as requestProperty values. Also tried adding it as parameters to the POST body but in both instances the request fails with a 401 error. Clearly the credentials are not being recognized although when used in POSTMAN request it works fine. I then duplicated the headers used in that request with the BASIC auth value and still the same error code. Perhaps I am not seeing the issue clearly:
obj = new URL(urlRequest);
conn = (HttpsURLConnection) obj.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("User-Agent", "PostmanRuntime/7.28.3");
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Content-Type","multipart/form-data");
//String userCredentials = "_username=xxxxxxxx&_password=yyyyyyyy";
//String basicAuth = "Basic " + new
String(Base64.getEncoder().encode(userCredentials.getBytes()));
//conn.setRequestProperty("Authorization", basicAuth);
conn.setRequestProperty("_username", "xxxxxxxxxx");
conn.setRequestProperty("_password", "yyyyyyyyyy");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("_username", "xxxxxxxxxx"));
params.add(new BasicNameValuePair("_password", "yyyyyyyyyy"));
String urlParameters = "_username=xxxxxxxxxx&_password=yyyyyyyyyy";
conn.setDoOutput(true);
try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
wr.writeBytes(urlParameters);
wr.flush();
}
InputStream urlInputStream;
int errCode = conn.getResponseCode();
BufferedReader inSt = new BufferedReader(new InputStreamReader(conn.getInputStream(),
"UTF-8"));
StringBuilder response = new StringBuilder();