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

Content-Length HTTP header is mis-capitalized

    XMLWordPrintable

Details

    • hopper
    • x86
    • windows_2000
    • Verified

    Description



      Name: nt126004 Date: 11/20/2001


      java version "1.4.0-beta3"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta3-b84)
      Java HotSpot(TM) Client VM (build 1.4.0-beta3-b84, mixed mode)

      Create an URLConectionHandler. Set doOutput to true and setMethod to POST. Get
      the output stream and write some bytes to the output stream. The bytes are
      written and the content length header is set, however the header is misspelled.
      According to the HTTP RFC (2616) Content-Length is capatilised as shown, from
      the 1.4 HTTPUrlConnection it is spelt as Content-length (note the lowercase 'l')

      Code
      ------
      import java.io.*;
      import java.net.*;

      public class TestCL implements Runnable
      {
          public static void main(String[] args) throws Exception
          {
              Thread t = new Thread(new TestCL());
              t.start();
              URL url = new URL("http://localhost:8888");
                                 
              HttpURLConnection con = (HttpURLConnection)url.openConnection();
              con.setRequestMethod("POST");
              con.setDoOutput(true);
              con.setRequestProperty("Content-Type", "application/x-www-form-
      urlencoded");
              
              // serialize to server
              String outputData = "email=" +
                  URLEncoder.encode("###@###.###") +
                  "&name=" +
                  URLEncoder.encode("fred")
                  +"&phone=" +
                  URLEncoder.encode("555-1234");
              
              OutputStream os = con.getOutputStream();
              os.write(outputData.getBytes());
              os.flush();
              os.close();
              con.connect();
              
              con.getInputStream();
          }

          public void run()
          {
              try
              {
                  ServerSocket ss = new ServerSocket(8888);
                  Socket socket = ss.accept();
                  BufferedReader br = new BufferedReader(new InputStreamReader
      (socket.getInputStream()));
                  String str = br.readLine();
                  while(str.length() != 0)
                  {
                      System.out.println(str);
                      str = br.readLine();
                  }
                  socket.close();
              }
              catch(Exception e)
              {
                  e.printStackTrace();
              }
          }
      }

      The thread loop prints the headers - you'll see the Content-length header being
      printes
      (Review ID: 135981)
      ======================================================================

      Attachments

        Activity

          People

            jccollet Jean-Christophe Collet (Inactive)
            nthompsosunw Nathanael Thompson (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: