Solved

Tomcat Inputstream Returns -1

Posted on 2009-04-08
6
301 Views
Last Modified: 2013-12-02
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?
0
Comment
Question by:lcor
  • 4
  • 2
6 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
whats the response code?

0
 

Author Comment

by:lcor
Comment Utility
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

0
 
LVL 92

Expert Comment

by:objects
Comment Utility
that looks ok, can you post your code

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:lcor
Comment Utility
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

0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
Comment Utility
then use HttpClient, it gives you greater control over what is sent.
nothing is going to give you exactly the samer behaviour as a browser though, plus every browser is different.

0
 
LVL 92

Expert Comment

by:objects
Comment Utility
why are you including cookies in the request?
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction (All good things must come to an end (http://en.wikipedia.org/wiki/All_Good_Things...)) The original MySQL API (http://php.net/manual/en/book.mysql.php) has gone away, deprecated by PHP in Version 5.5, and removed from PHP in all curre…
If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now