Link to home
Start Free TrialLog in
Avatar of lcor
lcor

asked on

Tomcat Inputstream Returns -1

I am trying to get content using Java's inputstream.  

When I try to get content from yahoo, sometimes the inputstream returns an immediate -1.  This happens when running in a servlet running on tomcat.

I ran a test prototype without the web server, tomcat 5.x, it works just fine.  Returns content.

Why would running in a web server make a difference?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

whats the response code?

Avatar of lcor
lcor

ASKER

HTTP/1.1 200 OK
Date: Wed, 08 Apr 2009 22:34:13 GMT
Cache-Control: max-age=315360000
Expires: Sat, 06 Apr 2019 22:34:13 GMT
Last-Modified: Thu, 06 Apr 2006 21:17:12 GMT
Accept-Ranges: bytes
Vary: Accept-Encoding
Content-Type: application/x-javascript
Content-Encoding: gzip
Age: 70101
Content-Length: 553
Proxy-Connection: keep-alive
Server: YTS/1.17.8

that looks ok, can you post your code

Avatar of lcor

ASKER

I tried using the URL class but did not get the behavior I need.  I need the exact same behavior as a browser so I have to use the Socket class. I've been posting questions and feedback has been to use the URL class.  I can't use the URL class cuz it doesn't give the same behavior as a browse.r
OutputStream os = null;
        ByteArrayOutputStream byteContainer = new ByteArrayOutputStream();
        try {
            Socket socket = new Socket("www.msnbc.msn.com", 80);
 
            String r = new String("GET / HTTP/1.1\n" +
                    "Host: www.msnbc.msn.com\n" +
                    "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12\n" +
                    "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\n" +
                    "Accept-Language: en-us,en;q=0.5\n" +
                    "Accept-Encoding: gzip,deflate\n" +
                    "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\n" +
                    "Keep-Alive: 300\n" +
                    "Connection: keep-alive\n" +
                    "Cookie: MC1=GUID=e72def902864496fa1074688e1697ab7; TZM=-240; __g_c=w%3A0; PageNo=1; s_cc=true; s_sq=%5B%5BB%5D%5D; MUID=F496E73B351C49BF8120BEECED8B6A98\n");
 
            System.out.println("Connecting using the following get request: " + r);
 
            os = socket.getOutputStream();
            boolean autoflush = true;
            PrintWriter out = new PrintWriter(socket.getOutputStream(), autoflush);
            out.println(new String(r));
            out.println('\r');
            out.println('\n');
            out.flush();
            socket.shutdownOutput();
 
            // Read the response
            byte[] b = new byte[1024];
            InputStream in = socket.getInputStream();
            int i = in.read(b);
            while (i != -1) {
                byteContainer.write(b, 0, i);
                i = in.read(b);
                System.out.println("i: " + i);
            }
 
            socket.close();
            in.close();
            out.close();
            os.close();
 
            byte[] content = byteContainer.toByteArray();
            byteContainer.close();
 
        } catch (IOException ex) {
            ex.printStackTrace();
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
why are you including cookies in the request?