401 Not Authorized Error when calling Common Gateway Interface program from a servlet
 Technote (troubleshooting)
 
Problem(Abstract)
This technote explains what to do if you receive a 401 error at the browser when you call a Common Gateway Interface (CGI) program from your servlet.
 
Cause
The authorization header might have been built incorrectly in the servlet before it made the request for the CGI.
 
Resolving the problem
If your code is similar to the following you can experience the problem:
byte[] rawupw = (new String("ibmuser:ibmuser")).getBytes();
String auth = "Basic " + new sun.misc.BASE64Encoder().encode(rawupw);
uc.setRequestProperty("Authorization", auth);
This code works on a machine that uses ASCII encoding, but encounters problems on the z/OSĀ® platform. The code creates an array of bytes in EBCDIC format when it runs on z/OS. (Since there were no arguments passed to the getBytes() call, the JavaTM Virtual Machine (JVM) takes the system default). It then builds the authorization header by using the EBCDIC bytes. Any request made with this header causes an HTTP server to return an error. The error occurs because an HTTP server receiving an authorization header expects the Base64 encoded data to be in ASCII, not EBCDIC.

In this particular case, the customer used the IBM HTTP Server on the z/OS platform

Resolve the problem by coding your servlet this way:
byte[] rawupw = (new String("ibmuser:ibmuser")).getBytes("ISO-8859-1");
String auth = "Basic " + new sun.misc.BASE64Encoder().encode(rawupw);
uc.setRequestProperty("Authorization", auth);

Note: the getBytes() call now overrides the default encoding, which used ASCII bytes.
 
 
 


Document Information


Current web document: swg21158211.html
Product categories: Software > Application Servers > Distributed Application & Web Servers > WebSphere Application Server for z/OS > Servlet Engine/Web Container
Operating system(s): z/OS
Software version: 5.0
Software edition:
Reference #: 1158211
IBM Group: Software Group
Modified date: May 7, 2004