JDK-4726087 : URLConnection cannot handle redirects
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 1.4.0,1.4.1
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: solaris_8,windows_2000
  • CPU: generic,x86
  • Submitted: 2002-08-05
  • Updated: 2002-10-11
  • Resolved: 2002-10-11
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
1.4.2 mantisFixed
Related Reports
Duplicate :  

Name: pa48320			Date: 08/05/2002

I am trying to write some code that accesses an https site by redirecting through a login site. When the httpsConnection follows the redirect I get an error that there is no protocol:

java.net.MalformedURLException: no protocol: /servlet/SessionServlet?ssaction=showLoginInvalid&url=/partner/bugs/data/bugs/4049125.html

        at java.net.URL.<init>(URL.java:579) 
        at java.net.URL.<init>(URL.java:476) 
        at java.net.URL.<init>(URL.java:425) 
        at sun.net.www.protocol.http.HttpURLConnection.followRedirect(HttpURLConnection.java:1074) 
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:668) 
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(DashoA6275) 
        at HTMLScrapeMain$HTMLScrapeMainInnerAdapter0.JButton1ActionPerformedHandler1(HTMLScrapeMain.java:369) 

If I turn off InstanceFollowsRedirect then I get a response back of "Temporarily Moved". This is in 1.4.1. Any ideas what needs to be set for this to work?

Here's the code: 

                                java.net.URL u = new java.net.URL("https", "javapartner.sun.com", -1, "/servlet/SessionServlet");

                                java.net.HttpURLConnection uc = (java.net.HttpURLConnection)u.openConnection(); 
//                              uc.connect(); 
                                // Make sure browser doesn't cache this URL. 

                                // Tell browser to allow me to send data to server. 
                        ByteArrayOutputStream byteStream = 
                                        new ByteArrayOutputStream(512); // Grows if necessary 
                                // Stream that writes into buffer 
                                PrintWriter out = new PrintWriter(byteStream, true); 
                                String postData = 
                                        "&url=" + URLEncoder.encode("/partner/bugs/data/bugs/4049125.html") + 
                                        "&UserId=" + URLEncoder.encode("<userID>")+ 

                                // Write POST data into local buffer 
                                out.flush(); // Flush since above used print, not println 

                                // POST requests are required to have Content-Length 
                                String lengthString = 
                                uc.setRequestProperty("Content-Length", lengthString); 

                                uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
                        // Write POST data to real output stream 

                                BufferedReader in = 
                                new BufferedReader(new InputStreamReader 
                        String line; 
                        String linefeed = "\n"; 
                        while((line = in.readLine()) != null) 


CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mantis FIXED IN: mantis INTEGRATED IN: mantis mantis-b04

WORK AROUND Name: pa48320 Date: 08/05/2002 Unknown ======================================================================

EVALUATION I think this is a server side error, most likely a bug in the servlet. The particular web server (javapartner.sun.com) or its component such as the servlet used by the test code doesn't seem to provide correct http responses. To redirect the client to a new place, the response header needs to contain a Location header with a value that correspond to an absolute url. But in this case, the value of the Location field is /servlet/SessionServlet?ssaction=showLoginInvalid&url=/partner/bugs/data/bugs/4049125.html, which is not an absolute URL. Thus when our http protocol handler is trying to parse it into an absolute URL, MalformedURLException is thrown. ###@###.### 2002-08-05 Although our implementation follows the HTTP standard, several popular browsers support relative URLs in the Location field for redirect. Thus we decide to make our client behaviour consistent with the popular browsers. Commit this to Mantis. ###@###.### 2002-08-12