Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Help with http 401 code ?

mycoffeeJun 8 2010 — edited Jun 9 2010
[Add] I forgot to mark this as question [Add]

I wrote a smalll program that send the xml to soap webservice on the server

If I use the browser to type the link on(http://myserver/myservices/RequestWS) , it pops up a window authenfication box. I type user name and password in and it works (logged in, see the message from the server)

However, if I run the java code below, the error is thorwn when I write to the outputStream.
public static void main(String[] args) throws Exception {


String SOAPUrl = args[0];
String xml = args[1];

URL url = new URL(SOAPUrl);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) connection;


byte[] b = xml.getBytes();
// Set the appropriate HTTP parameters.
httpConn.setRequestProperty( "Content-Length",String.valueOf( b.length ) );
httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");


httpConn.setRequestMethod( "POST" );
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setAllowUserInteraction(true);

httpConn.setDefaultUseCaches(false);
httpConn.setUseCaches(false);


OutputStream out = httpConn.getOutputStream();
System.out.println("httpConn.getResponseCode():"+httpConn.getResponseCode()); 
out.write( b ); 
out.close();

}
The console is:
httpConn.getResponseCode():401
java.io.IOException: Server returned HTTP response code: 401 for URL: http://ADMIN:password@myserver/myservices/RequestWS
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at SOAPClient.main(SOAPClient.java:81)
Caused by: java.io.IOException: Server returned HTTP response code: 401 for URL: http://ADMIN:password@myserver/myservices/RequestWS
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at SOAPClient.main(SOAPClient.java:74)
Exception in thread "main"

Please help.
Thank you very much

Edited by: mycoffee on Jun 8, 2010 10:27 AM

Edited by: mycoffee on Jun 8, 2010 11:06 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 7 2010
Added on Jun 8 2010
7 comments
2,130 views