FULL PRODUCT VERSION :
Java(TM) SE Runtime Environment (build 1.6.0_17-b04)
Java HotSpot(TM) Client VM (build 14.3-b01, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7600]
A DESCRIPTION OF THE PROBLEM :
Basic authorization fails when using HttpUrlConnection on Windows 7. Please note that the same code works on Linux and OSX. A packet trace reveals that the http header "Authorization" only has a "\r" carriage return and no "\n" line feed. Thus the server does not recognize the auth header and fails with a http 401.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the provided sample code on windows 7 (32 -bit), accessing a server that has basic authorization setup, using a valid user name and password.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
A proper http get request should be created and it should authenticate properly.
ACTUAL -
the server returns a 401 and an exception is thrown
ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.io.IOException: Server returned HTTP response code: 401 for URL: http://localhost:8080
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sorenson.threesixty.test.HttpUrlConnectionBugTest.main(HttpUrlConnectionBugTest.java:42)
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
package sorenson.threesixty.test;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import sorenson.threesixty.api.ThreeSixtyException;
import sorenson.threesixty.api.ThreeSixtyServiceResult;
import sun.misc.BASE64Encoder;
public class HttpUrlConnectionBugTest {
public static void main(String[] args) {
URL url;
StringBuilder sb = new StringBuilder();
String user = "user";
String passwd = "password";
String login = user + ":" + passwd;
String urlString = "http://localhost:8080";
try {
url = new URL(urlString);
HttpURLConnection conn;
String encodedLogin = new BASE64Encoder().encodeBuffer(login.getBytes());
encodedLogin = encodedLogin.replaceAll("\n", "");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", "basic " + encodedLogin);
conn.setRequestMethod("GET");
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} finally {
if (reader != null)
reader.close();
}
System.out.println(sb.toString());
String headerStatus = conn.getHeaderField("status");
String headerLocation = conn.getHeaderField("location");
System.out.println("status: " + headerStatus);
System.out.println("header location: " + headerLocation);
} catch (Exception e) {
e.printStackTrace();
}
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
No work around for windows 7.
Java(TM) SE Runtime Environment (build 1.6.0_17-b04)
Java HotSpot(TM) Client VM (build 14.3-b01, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7600]
A DESCRIPTION OF THE PROBLEM :
Basic authorization fails when using HttpUrlConnection on Windows 7. Please note that the same code works on Linux and OSX. A packet trace reveals that the http header "Authorization" only has a "\r" carriage return and no "\n" line feed. Thus the server does not recognize the auth header and fails with a http 401.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the provided sample code on windows 7 (32 -bit), accessing a server that has basic authorization setup, using a valid user name and password.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
A proper http get request should be created and it should authenticate properly.
ACTUAL -
the server returns a 401 and an exception is thrown
ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.io.IOException: Server returned HTTP response code: 401 for URL: http://localhost:8080
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sorenson.threesixty.test.HttpUrlConnectionBugTest.main(HttpUrlConnectionBugTest.java:42)
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
package sorenson.threesixty.test;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import sorenson.threesixty.api.ThreeSixtyException;
import sorenson.threesixty.api.ThreeSixtyServiceResult;
import sun.misc.BASE64Encoder;
public class HttpUrlConnectionBugTest {
public static void main(String[] args) {
URL url;
StringBuilder sb = new StringBuilder();
String user = "user";
String passwd = "password";
String login = user + ":" + passwd;
String urlString = "http://localhost:8080";
try {
url = new URL(urlString);
HttpURLConnection conn;
String encodedLogin = new BASE64Encoder().encodeBuffer(login.getBytes());
encodedLogin = encodedLogin.replaceAll("\n", "");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", "basic " + encodedLogin);
conn.setRequestMethod("GET");
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} finally {
if (reader != null)
reader.close();
}
System.out.println(sb.toString());
String headerStatus = conn.getHeaderField("status");
String headerLocation = conn.getHeaderField("location");
System.out.println("status: " + headerStatus);
System.out.println("header location: " + headerLocation);
} catch (Exception e) {
e.printStackTrace();
}
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
No work around for windows 7.