Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4726087
Votes 7
Synopsis URLConnection cannot handle redirects
Category java:classes_net
Reported Against 1.4 , hopper
Release Fixed 1.4.2(mantis)
State 10-Fix Delivered, bug
Priority: 4-Low
Related Bugs 4786588
Submit Date 05-AUG-2002
Description



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(); 


======================================================================
Work Around



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.

 xxxxx@xxxxx  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.

 xxxxx@xxxxx  2002-08-12
Comments
  
  Include a link with my name & email   

Submitted On 05-SEP-2002
albanard69
I get the same error, using the http protocol,  but only
when one of the request parameters contains a "%3D" ( the
encoded version of "=") or  "%2B" (the encoded version of
"+") imediately followed by characters.  The same error does
not occur if there is a "+" ( encoded version of space) on
either side of these characters. 

This may be because the site I'm trying to connect to only
redirects when these special characters are present, but I
thought I'd mention it anyway.

Do any previous versions of java follow relative redirects?


Submitted On 11-DEC-2002
hson
As far as I can tell the only version affected by this "bug" is 
1.4.1, 1.3.1_06 and 1.4.0_01 on Windows plus 1.3.0 and 
1.4.0 on Solaris(both sparc and x86) are not affected by this.


Submitted On 05-FEB-2003
albanard69
Thanks hson,

Ignore request parameters section of my comment sep 5 2002.
 Has nothing to do with request parameters.

1.4.0_03 seems to work fine for redirects as well.


Submitted On 13-MAR-2003
alxstar
Relative redirection URLs are supproted (and widely used!) in 
IIS 5.0.
I have a real problem trying to communicate with some third-
party site running IIS 5.0. JDK 1.4.1 on Windows cannot 
redirect when HTTP error 302 is received. However, 1.3.1 
redirects fine in the same environment.


Submitted On 22-MAY-2003
djking
ummm the bug is still there as of 1.4.2 beta, what's up with
that?  I have an app that works un 1.3.1 but breaks under
all versions of 1.4.x that I've tried.  When will this fixed
bug make it into production?


Submitted On 11-JUN-2003
djking
This bug has not been fixed as of 1.4.2 beta, I recomend 
people switch their votes to bug# 4800036.


Submitted On 13-JUN-2003
yingxian
djking,

Relative redirect of http should work in 1.4.2 beta.
However, maybe you encountered some special cases that we
overlooked.

Could you please elaborate on the problems you encounter in
detail? for instance, send us both the origin url and the
url to be directed to. A sample program that demonstrate the
problem would be best.

Please sent it to java-net@sun.com.

Thanks


Submitted On 24-JUN-2003
djking
yingxian - I tried again with 1.4.2 b2 and it worked, so
either I did not istall the beta properly the first time or
 it got fixed between b1 - b2.

- Peace


Submitted On 28-JUN-2003
annhoody
i'm trying  to get  java  so  i can  play  on  line  keep  getting 
error's?



PLEASE NOTE: JDK6 is formerly known as Project Mustang