I'm doing a very simple GET operation using regular (non-SSL) http access. I call the Method.getResponseBodyAsStream() method and do a couple of reads to read a few bytes. My third read fails with exception "java.io.IOException: Attempted read on closed stream", yet I have not closed the stream.
The URL I am trying to get the content for is: http://images.amazon.com/images/P/MEDIASERVE.01.LZZZZZZZ.jpg and it is successfully getting the first 2 bytes 'GI', but then failing after that. Here's my code: HttpClient client = new HttpClient(); GetMethod getMethod = new GetMethod(contentUrl); try { client.executeMethod(getMethod); if (getMethod.getStatusCode() == HttpStatus.SC_OK) { contentStream = getMethod.getResponseBodyAsStream(); System.out.println("contentStream 2: " + contentStream); try { System.out.println("20 Read byte " + contentStream.read()); } catch (Exception e) { System.out.println("20 Failed to read bytes"); } try { System.out.println("21 Read byte " + contentStream.read()); } catch (Exception e) { System.out.println("21 Failed to read bytes"); } System.out.println("GET succeeded"); contentLength = 17949; contentType = "image/gif"; } else { throw new AWSException("CannotGetContent", getMethod.getStatusText()); } } catch (IOException e) { throw new AWSException("CannotGetContent", e.getMessage()); } finally { getMethod.releaseConnection(); } } // Transaction to prepare for content upload System.out.println("contentStream 3: " + contentStream); try { System.out.println("22 Read byte " + contentStream.read()); } catch (Exception e) { System.out.println("22 Failed to read bytes"); } } Here is the output I get: contentStream 2: [EMAIL PROTECTED] 20 Read byte 71 21 Read byte 73 GET succeeded contentStream 3: [EMAIL PROTECTED] 22 Failed to read bytes -----Original Message----- From: Roland Weber [mailto:[EMAIL PROTECTED] Sent: Tuesday, February 07, 2006 10:40 PM To: HttpClient User Discussion Subject: Re: Connection gets closed on GET operation Hi Keith, > I'm doing a very simple GET operation using regular (non-SSL) http access. I > call the Method.getResponseBodyAsStream() method and do a couple of > reads to read a few bytes. > My third read fails with exception "java.io.IOException: Attempted > read on closed stream", yet I have not closed the stream. The stream is connecting your client to a server. It can be closed by your application reading to EOF, by your application releasing the connection you used for executing the GET method, or by the server. You have to provide more specific information about what you are doing if you want us to help. cheers, Roland > > I'm noting that the connection is of > class org.apache.commons.httpclient.AutoCloseInputStream, so maybe > it's self > closing? I've looked at the code for this class and it says that it closes the > Connection when no more content, but I have only read the first couple > of bytes of data. > > -Keith > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
