United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-4726087 URLConnection cannot handle redirects
JDK-4726087 : URLConnection cannot handle redirects

Details
Type:
Bug
Submit Date:
2002-08-05
Status:
Resolved
Updated Date:
2002-10-11
Project Name:
JDK
Resolved Date:
2002-10-11
Component:
core-libs
OS:
solaris_8,windows_2000
Sub-Component:
java.net
CPU:
x86,generic
Priority:
P4
Resolution:
Fixed
Affected Versions:
1.4.0,1.4.1
Fixed Versions:
1.4.2 (mantis)

Related Reports
Duplicate:

Sub Tasks

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



Name: pa48320			Date: 08/05/2002

Unknown
======================================================================
                                     
2004-06-11
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
mantis

FIXED IN:
mantis

INTEGRATED IN:
mantis
mantis-b04


                                     
2004-06-14



Hardware and Software, Engineered to Work Together