Cannot add Authorization field to HttpsUrlConnection in order to complete Basic authentication

Posted by ES on Stack Overflow See other posts from Stack Overflow or by ES
Published on 2010-03-15T15:31:27Z Indexed on 2010/03/15 17:59 UTC
Read the original article Hit count: 685

Filed under:

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&param2=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

© Stack Overflow or respective owner

Related posts about urlconnection