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

HttpURLConnection doesn't implement If-Modified-Since Header

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.2.0
    • 1.1.4
    • core-libs
    • 1.2beta4
    • sparc
    • solaris_2.5.1
    • Not verified



      Name: paC48320 Date: 11/13/97


      The JDK http handler implementation class:

          sun.net.www.protocol.HttpURLConnection

      ignores any "IfModifiedSince" value that was set by calling

          java.net.URLConnection.setIfModifiedSince(long)

      In other words, it fails to write the HTTP Header entry that,
      according to RFC1945, should look like:

         If-Modified-Since = "If-Modified-Since" ":" HTTP-date

      Run the module supplied below, as follows:

          % java HTTPTest http://<someValidURL>

      And you should see a response code of 304 since I set the
      if-modified-since date to be way in the future.

      Instead, you'll see a response code of 200.

      NOTE: If you look inside of sun.net.www.protocol.http.HttpURLConnection
            you will see that there is no code to handle
            the setting of the If-Modified-Since header entry.

            The code in writeRequests should look something like:

            if ( getModifiedSince() != 0 )
            {
                requests.setIfNotSet("If-Modified-Since", getModifiedSince());
            }


      Except that you will have to use DateFormat to convert the getModifiedSince()
      date into the HTTP-Date String format required by HTTP.


      //
      // Test module for broken If-Modified-Since HTTP Header...
      //
      // ###@###.###
      //
      import java.net.*;

      public class HTTPTest extends Object
      {
          public HTTPTest()
          {
          }

          public static final void main(String argv[])
          {
              try
              {
                  URL testURL = new URL(argv[0]);
                  URLConnection URLConn = testURL.openConnection();
                  HttpURLConnection httpConn;

                  if ( URLConn instanceof HttpURLConnection )
                  {
                      httpConn = (HttpURLConnection)URLConn;
                      httpConn.setAllowUserInteraction(false);
                      httpConn.setIfModifiedSince(999000000000l);
                      httpConn.connect();

                      System.out.println("URL:" + httpConn.getURL()
                                         + "\nContent-encoding:"
                                         + httpConn.getContentEncoding()
                                         + "\nContent-length:"
                                         + httpConn.getContentLength()
                                         + "\nContent-type:"
                                         + httpConn.getContentType()
                                         + "\nDate:"
                                         + httpConn.getDate()
                                         + "\nIf-Modified-Since:"
                                         + httpConn.getIfModifiedSince()
                                         + "\nLast-Modified:"
                                         + httpConn.getLastModified());
                      System.out.println(httpConn.getRequestMethod()
                                         + " " + httpConn.getResponseCode()
                                         + " " + httpConn.getResponseMessage());

                      for ( int i = 0; ;i++ )
                      {
                          httpConn.getHeaderField(i);
                          if ( httpConn.getHeaderFieldKey(i) == null )
                          {
                              break;
                          }

                          System.out.println(httpConn.getHeaderFieldKey(i)
                                             + ":"
                                             + httpConn.getHeaderField(i));
                      }
                  }
                  else
                  {
                      System.out.println("URL is not a cache candidate.");
                  }
              }
              catch (Exception e)
              {
                  e.printStackTrace();
              }
          }
      }
      (Review ID: 19884)
      ======================================================================

            mmcclosksunw Michael Mccloskey (Inactive)
            pallenba Peter Allenbach (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: