I fixed the problem with the help of others. Here is the tip I got:
/** post data to an url that requires authorization and get a response as well
@param urlstr the complete url, including
http:// @param username the username
@param password the password
@param parameters the parameters in unencoded-url format (param1=value¶m2=value etc.)
@return the response generated by the url from the post
*/
public static String postDataAuthorize(String urlstr, String parameters, String username, String password)
throws IOException
{
URL url = new URL(urlstr);
HttpURLConnection hpcon = null;
try{
String userPassword = username + ":" + password;
String encoding = new sun.misc.BASE64Encoder().encode (userPassword.getBytes());
hpcon = (HttpURLConnection) url.openConnection();
hpcon.setRequestMethod("POST");
hpcon.setRequestProperty("Content-Length", "" + Integer.toString(parameters.getBytes().length));
hpcon.setRequestProperty ("Authorization", "Basic " + encoding);
//hpcon.setRequestProperty("User-Agent", "MLI/Java development environment");
hpcon.setRequestProperty("Content-Language", "en-US");
hpcon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
hpcon.setUseCaches (false);
hpcon.setDoInput(true);
hpcon.setDoOutput(true);
DataOutputStream printout = new DataOutputStream (hpcon.getOutputStream ());
printout.writeBytes (parameters);
printout.flush ();
printout.close ();
// getting the response is required to force the request, otherwise it might not even be sent at all
BufferedReader in = new BufferedReader(new InputStreamReader(hpcon.getInputStream()));
String input;
StringBuffer response = new StringBuffer(256);
while((input = in.readLine()) != null) {
response.append(input + "\r");
}
return response.toString();
} catch(Exception e){
throw new IOException(e.getMessage());
} finally {
if(hpcon != null){
hpcon.disconnect();
}
}
}