Cannot add Authorization field to HttpsUrlConnection in order to complete Basic authentication
- by ES
Hi,
I'm using the Sun API HttpsURLConnection class, and have been trying for a day now to get it to send a simple request:
URL url = new URL("https://thirdpartyserver.com/somelocation");
connection = (HttpsURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
if (doAuthorization) {
Base64Converter converter = new Base64Converter();
connection.setRequestProperty("Authorization",
"Basic " + converter.encode("username:password"));
}
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write("param1=100¶m2=hello");
writer.flush();
writer.close();
I keep getting 401 from the third part server. When I look at the connection through the debugger, the method shows up as GET even though I set it to POST; the collection of request properties shows up as null.
If I print the values out, the method shows up as POST, but the request parameters collection is still empty.
I would love to be able to print the request and understand what's going on, but I could not figure out how to print the content of an output buffer.
Any ideas?
Thanks!
ES