This discussion is archived
7 Replies Latest reply: Oct 10, 2012 12:44 AM by 967122 RSS

Reading from a URLConnection in Java 7

967122 Newbie
Currently Being Moderated
Hi,

I have started to get a problem with reading a response from a URLConnection. This code have been running for years without a problem until I switched to Java 7. When I read the response using Java 7 Update 7 it sometimes works and then other times it just hangs. It will read part of the response and then just hang on a channel.read call. It's seems to be totally random when it hangs and when it works. I have built a small program to duplicate the issue and attached below to read a simple RSS URL feed. If the program is run with Java 6 (Update 35) it works correctly all the time, 100 connections all read correctly. If the program is run with Java 7, it fails on average between 8 - 10 times per 100 connections. I have set the urlConnection to time out after 10 secs to stop the program from hanging. Has anybody had similar problems with reading URLConnections in Java 7?

Thanks,
Steve.

package com.test.network;

import java.nio.*;
import java.nio.channels.*;
import java.net.*;

public class URLResponseReader {

     public static void main(String[] args) {
          System.out.println("Java version: " + System.getProperty("java.version"));
          int loopFor = 100;
          int noOfFails = 0;
          for (int i=0; i<loopFor; i++) {
               try {
                    URL url = new URL("http://rss.slashdot.org/Slashdot/slashdot");
                    
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
     urlConnection.setUseCaches(false);
     urlConnection.setDoOutput(true);
     
     // set a timeout here to get pass the read hanging forever!
     urlConnection.setReadTimeout(10000);
     
                    ReadableByteChannel channel = Channels.newChannel(urlConnection.getInputStream());
                    ByteBuffer buffer = ByteBuffer.allocate(1024);
                    int noOfBytes = 0;
                    byte[] data = null;
                    StringBuffer response = new StringBuffer();
                    String line = null;
                    while ((noOfBytes = channel.read(buffer)) > 0) {
                         data = new byte[noOfBytes];
                    System.arraycopy(buffer.array(), 0, data, 0, noOfBytes);
                         buffer.clear();
                         line = new String(data);
     
     response.append(line);
                    }
               channel.close();
               System.out.print(".");
               }
               catch (Exception ex) {
System.out.print(ex.getMessage());
                    noOfFails++;
               }
          }
          System.out.println("");
System.out.println("No of fails: " + noOfFails + " out of " + loopFor);
          System.exit(0);
     }
}

Edited by: user5872811 on 09-Oct-2012 06:01
  • 1. Re: Reading from a URLConnection in Java 7
    sabre150 Expert
    Currently Being Moderated
    Have only run it once but get


    Java version: 1.7.0_07
    ....................................................................................................
    No of fails: 0 out of 100


    Ubuntu Linux 11.04
  • 2. Re: Reading from a URLConnection in Java 7
    967122 Newbie
    Currently Being Moderated
    I'm running on Windows 7 32-bit. I see you are ruuning it on Ubuntu. I have a ubuntu machine as well. I'll try it on that.
    It works perfect on Java 6:

    Java version: 1.6.0_35
    Reading URL using NIO Channel reader.
    ....................................................................................................
    No of fails: 0 out of 100

    ======================================

    Reading URL using BufferedReader.
    ....................................................................................................
    No of fails: 0 out of 100

    But with Java 7 I get:

    Java version: 1.7.0_07
    Reading URL using NIO Channel reader.
    .Read timed out...................................Read timed out.....................Read timed out................Read timed out.......................
    No of fails: 4 out of 100

    ======================================

    Reading URL using BufferedReader.
    Read timed out..........Read timed out..Read timed out...........Read timed out.........Read timed out..Read timed out.Read timed out..........Read timed out...................Read timed outRead timed out............Read timed out.....Read timed out.......
    No of fails: 12 out of 100
  • 3. Re: Reading from a URLConnection in Java 7
    sabre150 Expert
    Currently Being Moderated
    No problem either running on Xp with 1.7.0_07 .
  • 4. Re: Reading from a URLConnection in Java 7
    967122 Newbie
    Currently Being Moderated
    Hi,

    I have tried it on Ubuntu now myelf and it works fine. So I tried it on my macbook and on a Windows 8 64-bit and it all works fine. The problem appears to be only on my Windows 7 32-bit machine. You don't have one of those my any chance to see if you can duplicate it on Win 7 32-bit? I got one of the lads in the office who also has a win 7 32-bit machine and he gets the same problem I get.

    Thanks,
    Steve.
  • 5. Re: Reading from a URLConnection in Java 7
    sabre150 Expert
    Currently Being Moderated
    Also no problem running under 1.7.0_07 on Windows 7 32 bit hosted on Virtual Box.
  • 6. Re: Reading from a URLConnection in Java 7
    967122 Newbie
    Currently Being Moderated
    Thank you for the testing it on Win 7 32-bit. Not sure what I'll do next. I'll try it outside my office tonight to see if it's something in our network that might be causing it.

    Thanks,
    Steve.
  • 7. Re: Reading from a URLConnection in Java 7
    967122 Newbie
    Currently Being Moderated
    It's AVG 9.0 Online Shield and Link Scanner that causes the issue on Win 7 32 and 64 bit machines. Once I disabled these the URLConnection reads never fail.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points