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.
Other
1.4.2 mantisFixed
Related Reports
Duplicate :  
Description

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.setRequestMethod("POST"); 
//                              uc.connect(); 
                                // Make sure browser doesn't cache this URL. 
                                uc.setUseCaches(false); 
                                uc.setFollowRedirects(false); 
                                uc.setInstanceFollowRedirects(false); 

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

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

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

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

                                BufferedReader in = 
                                new BufferedReader(new InputStreamReader 
                             (uc.getInputStream())); 
                        String line; 
                        String linefeed = "\n"; 
                        outField.setText(""); 
                        while((line = in.readLine()) != null) 
                                { 
                                outField.append(line); 
                                outField.append(linefeed); 
                        } 
                                System.out.println(uc.getResponseMessage()); 
                                uc.disconnect(); 


======================================================================

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

WORK AROUND Name: pa48320 Date: 08/05/2002 Unknown ======================================================================
11-06-2004

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
12-08-2002