Authorization Fails whenever we try to connect to a URL(which requires Basic Authentication) & send data through
URLConnection.(Post Method)

We are opening an URLConnection to a URL which requires Basic Authentication.When we try to send data to this URL via a DataOutputStream,the Authentication fails. We are setting the following header for Authentication.

// For Basic Authentication
 String password= "UserName" +":" +"Password";
 String auth = "Basic " + new sun.misc.BASE64Encoder().encode(password.getBytes());
 con.setRequestProperty( "Authorization", auth);

We are Using the following Code :

Note: toEncodedString(strQstring) :-- does the URLEncoder.encode of strQstring.

String argString = toEncodedString("ParamName1=value1&ParamName2=value2");
URL servlet = new URL ("http://myserver/........");

URLConnection con = servlet.openConnection();

con.setDoInput(true);
con.setDoOutput(true);
// Turn off caching
 con.setUseCaches(false);
 con.setAllowUserInteraction(true);
 con.setRequestProperty("Content-Type",
        "application/x-www-form-urlencoded");

// For Basic Authentication
 String password= "UserName" +":" +"Password";
 String auth = "Basic " + new sun.misc.BASE64Encoder().encode(password.getBytes());
 con.setRequestProperty( "Authorization", auth);

 // Write the arguments as post data
DataOutputStream out = new DataOutputStream(con.getOutputStream());
out.writeBytes(argString);
out.flush();
out.close();

InputStream inputStream = con.getInputStream();

We are getting the following Error when we execute this.

###################################################
weblogic.net.http.HttpUnauthorizedException: 401
        at weblogic.net.http.HttpURLConnection.getInputStream(HttpURLConnection.
java:315)
###################################################

But the same code works fine when we pass the data as part of URL Query string.(GET Method)

Note: toEncodedString(strQstring) :-- does the URLEncoder.encode of strQstring.

String argString = toEncodedString("ParamName1=value1&ParamName2=value2");
URL servlet = new URL ("http://myserver/........");

StringBuffer url =new StringBuffer(servlet.toExternalForm());
 url.append("&");
 url.append(argString);
servlet = new URL(url.toString());

 URLConnection con = servlet.openConnection();
 // Prepare for both input and output
 con.setDoInput(true);
 con.setDoOutput(true);
 // Turn off caching
 con.setUseCaches(false);
 con.setRequestProperty("Content-Type",
        "application/x-www-form-urlencoded");
 String password= "UserName" +":" +"Password";
 String auth = "Basic " + new sun.misc.BASE64Encoder().encode(password.getBytes());
 con.setRequestProperty( "Authorization", auth);
 con.setAllowUserInteraction(true);

InputStream inputStream = con.getInputStream();

The above Code works fine.
It fails only when we try to pass data through outputstream.

This Authorization works in Normal JDK1.3 Environment (where java.protocol.handler.pkgs =
sun.net.www.protocol.http.Handler)
But the same thing fails in WebLogic Environment.(where java.protocol.handler.pkgs = weblogic.net...)


Thanks in advance....