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

Use of HttpURLConnection results in hang when same request on Socket does not

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P4 P4
    • None
    • 1.2.0
    • core-libs
    • x86
    • windows_nt



      Name: dbT83986 Date: 03/25/99


      The hostnames have been modified in the following URLs for
      security and privacy reasons.

      This code:

        PrintWriter responseWriter = null ;
        try {
          URL url = new URL ( "http://machine.domain.tld:81/oracle/app/dad/utility.framework.under_construction" );
          Socket webSocket = new Socket ( url.getHost(), url.getPort() ) ;
          webSocket.setSoTimeout ( 30000 ) ;
          PrintWriter requestWriter = new PrintWriter ( webSocket.getOutputStream (), true ) ;

          requestWriter.println ( "POST " + url.getFile() + "HTTP/1.1" ) ;
          requestWriter.println ( "Content-Type: application/x-www-form-urlencoded" );
          requestWriter.println ( "Content-Length: " + parameters.length() ) ;
          requestWriter.println ( "Cookie: $Version = \"0\"; SessionID=" + session_id + "; $Path=\"/\"" );
          requestWriter.println () ;
          requestWriter.println ( parameters ) ;

          responseWriter = new PrintWriter ( response.getWriter ( ) ) ;
          BufferedReader resultReader = new BufferedReader ( new InputStreamReader ( webSocket.getInputStream ( ) ) ) ;
          char buffer[] ;
          buffer = new char[8192] ;
          int charsRead = 0 ;
          String lineRead = null ;

          while ( (lineRead = resultReader.readLine ()) != null ) {
            responseWriter.println (lineRead);
          }

          responseWriter.flush ( ) ;
          responseWriter.close ( ) ;
        } catch ( Exception e ) {
          response.setContentType ( "text/html" ) ;
          responseWriter.println ( "<h2>" + e.getMessage() + "</h2>" ) ;
          responseWriter.println ( "<pre>" ) ;
          e.printStackTrace ( responseWriter ) ;
          responseWriter.println ( "</pre>" ) ;
        }

      does what it is expected to do and delievers the page. The
      following code, which I understand should behave exactly the
      same way, does not:

        URL url = new URL ( "http://machine.domain.tld:81/oracle/app/dad/utility.framework.under_construction" ) ;
        HttpURLConnection urlConn = null ;

        try {
          urlConn = (HttpURLConnection) url.openConnection () ;
          urlConn.setRequestMethod ( "POST" ) ;
          urlConn.setRequestProperty ( "Content-Type", "application/x-www-form-urlencoded" );
          urlConn.setRequestProperty ( "Cookie", "$Version = \"0\"; SessionID=" + session_id + "; $Path=\"/\"" );
          urlConn.setDoInput ( true );
          urlConn.setDoOutput ( true );
          urlConn.setUseCaches ( false );
          urlConn.setFollowRedirects ( false ) ;

          <<<<<< HANG HERE >>>>>>>
       
         try {
           contentType = urlConn.getContentType () ;
           httpResponseCode = urlConn.getResponseCode () ;
           httpResponseMessage = urlConn.getResponseMessage ( ) ;
          } catch ( Exception e ) {
           System.err.println ( new Date().toString() +
             "\tException thrown on attempt #" + (++attempts ) +
             ". \n\t\t\tMessage: " + e.getMessage () +
             "\n\t\t\tClassName: " + e.getClass().getName() ) ;
          }


          PrintWriter requestWriter = new PrintWriter ( urlConn.getOutputStream (), true );
          BufferedReader resultReader = new BufferedReader ( new InputStreamReader ( webSocket.getInputStream ( ) ) ) ;
          char buffer[] ;
          buffer = new char[8192] ;
          int charsRead = 0 ;
          String lineRead = null ;

          while ( (lineRead = resultReader.readLine ()) != null ) {
            responseWriter.println (lineRead);
          }

          responseWriter.flush ( ) ;
          responseWriter.close ( ) ;
        } catch ( Exception e ) {
          response.setContentType ( "text/html" ) ;
          responseWriter.println ( "<h2>" + e.getMessage() + "</h2>" ) ;
          responseWriter.println ( "<pre>" ) ;
          e.printStackTrace ( responseWriter ) ;
          responseWriter.println ( "</pre>" ) ;
        }

      When I reported earlier I heard back from Gregory Stone on
      Review ID #55986 that I should contact technical support with
      a more specific chunk of code. Here it is.

      =======================
      REVIEW NOTE 4/22/99 - User sent in additional information

      This is still an issue for us. The code in the try block is in the wrong
      place because of cut-and-paste stupidity when I put this report together.

      It did appear in my code after the parameters where written. Here is the
      piece of code that does the work today (under JDK 1.1) but did not work
      when I moved the code over the JDK 1.2:

      try {
        urlConn = (HttpURLConnection) url.openConnection () ;
        urlConn.setDoInput ( true );
        urlConn.setDoOutput ( true );
        urlConn.setUseCaches ( false );
        urlConn.setFollowRedirects ( false ) ;
        urlConn.setRequestProperty ( "Cookie",
                                     "$Version = \"0\"; SessionID=" + session_id
      + "; $Path=\"/\"" );
        urlConn.setRequestProperty ( "Content-Type",
      "application/x-www-form-urlencoded" );
        
        OutputStreamWriter requestWriter = new OutputStreamWriter (
      urlConn.getOutputStream () );

        //
        // Send the POST data
        //

        requestWriter.write ( parameters );
        requestWriter.flush ( );
        requestWriter.close ( );
        
      } catch (IOException ioExcept ) {
        throw new IOException ( ioExcept.getMessage() + ": " + requestURL ) ;
      }
          
      System.out.println ( "Application Request ( attempt #" + attempts + "): " +
      urlString ) ;

      /* hangs here */

      try {
        httpResponseCode = urlConn.getResponseCode () ;
        httpResponseMessage = urlConn.getResponseMessage ( ) ;
      } catch ( Exception e ) {
        System.err.println ( "Server didn't provide response code and/or message,
      attempt #: " + (++attempts)) ;
        continue ;
      }

      This code works on JDK 1.1.
      (Review ID: 56036)
      ======================================================================

            ywangsunw Yingxian Wang (Inactive)
            dblairsunw Dave Blair (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: