Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6579178

setRequestProperty is not working

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 6
    • core-libs
    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      java version "1.6.0_02"
      Java(TM) SE Runtime Environment (build 1.6.0_02-b05)
      Java HotSpot(TM) Client VM (build 1.6.0_02-b05, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Windows XP SP1 with security upgrades.

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Run-of-the-mill AMD 32 PC.

      A DESCRIPTION OF THE PROBLEM :
      I'm trying to write a program to access password protected servers (a WebMail server, for example). I'm using Eclipse 3.2.2. I tried the following code:

      ...
      ConnAuth=new sun.misc.BASE64Encoder().encode (ArgStr[Cnt].getBytes());
      URL SecureURL;
      URLConnection Conn;
      SecureURL=new URL("http://myrealbox.com/");
      Conn=SecureURL.openConnection();
      Conn.setConnectTimeout(60000);
      Conn.setReadTimeout(120000);
      Conn.setRequestProperty("Cookie","style=standard");
      Conn.setRequestProperty("Referer","http://www.myrealbox.com/");
      Conn.setRequestProperty("User-Agent","Mozilla/5.0");
      Conn.setRequestProperty("Authorization","Basic "+ConnAuth);
      System.out.println("Tron: "+Conn.getRequestProperties());
      Conn.connect();

      The 'System.out.println' line was introduced only to trace the problem, printing out this:

      Tron: {Cookie=[style=standard], Host=[myrealbox.com], Referer=[http://www.myrealbox.com/], User-Agent=[Mozilla/5.0]}

      The code didn't work, so I tried a suggested variation (in a Forum):

      ...
      URL SecureURL;
      HttpURLConnection Conn;
      SecureURL=new URL("http://myrealbox.com/");
      Conn=(HttpURLConnection)SecureURL.openConnection();
      ...

      That also didn't work. There are, actually, two problems with the code:

      1) Everything I set with 'setRequestProperty' is printed out correctly on the tracer line, EXCEPT the most important "Authorization" field! In other words, for some reason, the "Authorization" field is being discarded from the header set (or so it seems).

      2) I thought that, for security reasons or whatever, the "Authorization" field was set, but 'getRequestProperties' couldn't retrieve it, so I used Ethereal (ethereal.com) to capture network traffic. This was the result:

      GET / HTTP/1.1
      User-Agent: Java/1.6.0_02
      Host: myrealbox.com
      Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
      Connection: keep-alive

      It's obvious that, although 'getRequestProperties' printed most of the fields I had set (except for "Authorization"), no one was sent to the server! "User-Agent", for example, wasn't set to "Mozilla/5.0" as specified in the code.

      Am I missing something in my code? It's basically the same code I found in dozens of sources (including examples on sun.com). Is that the correct way to set the HTTP header?


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the program below. Capture the output with Ethereal program (configure to "tcp port http" monitoring).

      package Body;

      import java.net.*;
      import java.io.*;

      public class Probe
      { public static void main(String[] ArgStr) throws InterruptedException
      {
      try
      { String ConnAuth=new sun.misc.BASE64Encoder().encode(ArgStr[0].getBytes());
      URL SecureURL;
      URLConnection Conn;
      SecureURL=new URL("http://www.mamechannel.it/files/samples/bowl3d.zip");
      Conn=SecureURL.openConnection();
      Conn.setConnectTimeout(60000);
      Conn.setReadTimeout(120000);
      Conn.setRequestProperty("Cookie","style=grigio");
      Conn.setRequestProperty("Referer","http://www.mamechannel.it/");
      Conn.setRequestProperty("User-Agent","Mozilla/5.0");
      Conn.setRequestProperty("Authorization","Basic "+ConnAuth);
      System.out.println("Tron: "+Conn.getRequestProperties());
      Conn.connect();
      BufferedReader In=new BufferedReader(new InputStreamReader(SecureURL.openStream(),"ISO-8859-1"));
      StringBuffer Buffer=new StringBuffer("");
      String InputLine;

      while ((InputLine = In.readLine()) != null)
      Buffer.append(InputLine);
      In.close();
      System.out.println(Buffer);
      } catch (MalformedURLException Error)
      { System.err.println("*** URL Error: "+Error);
      } catch (IOException Error)
      { System.err.println("*** Access I/O Error: "+Error);
      }
      }
      }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The program should get access to the file and download it.
      ACTUAL -
      Actual program output:

      Tron: {Cookie=[style=grigio], Referer=[http://www.mamechannel.it/], User-Agent=[Mozilla/5.0]}
      *** Access I/O Error: java.io.IOException: Server returned HTTP response code: 401 for URL: http://www.mamechannel.it/files/samples/bowl3d.zip

      Network packages captured with Ethreal:

      GET /files/samples/bowl3d.zip HTTP/1.1
      User-Agent: Java/1.6.0_02
      Host: www.mamechannel.it
      Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
      Connection: keep-alive

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      *** Access I/O Error: java.io.IOException: Server returned HTTP response code: 401 for URL: http://www.mamechannel.it/files/samples/bowl3d.zip

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package Body;

      import java.net.*;
      import java.io.*;

      public class Probe
      { public static void main(String[] ArgStr) throws InterruptedException
      {
      try
      { String ConnAuth=new sun.misc.BASE64Encoder().encode(ArgStr[0].getBytes());
      URL SecureURL;
      URLConnection Conn;
      SecureURL=new URL("http://www.mamechannel.it/files/samples/bowl3d.zip");
      Conn=SecureURL.openConnection();
      Conn.setConnectTimeout(60000);
      Conn.setReadTimeout(120000);
      Conn.setRequestProperty("Cookie","style=grigio");
      Conn.setRequestProperty("Referer","http://www.mamechannel.it/");
      Conn.setRequestProperty("User-Agent","Mozilla/5.0");
      Conn.setRequestProperty("Authorization","Basic "+ConnAuth);
      System.out.println("Tron: "+Conn.getRequestProperties());
      Conn.connect();
      BufferedReader In=new BufferedReader(new InputStreamReader(SecureURL.openStream(),"ISO-8859-1"));
      StringBuffer Buffer=new StringBuffer("");
      String InputLine;

      while ((InputLine = In.readLine()) != null)
      Buffer.append(InputLine);
      In.close();
      System.out.println(Buffer);
      } catch (MalformedURLException Error)
      { System.err.println("*** URL Error: "+Error);
      } catch (IOException Error)
      { System.err.println("*** Access I/O Error: "+Error);
      }
      }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      I tried many things, none worked.

            chegar Chris Hegarty
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: