Solved

Tomcat Inputstream Returns -1

Posted on 2009-04-08
6
315 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24103571
whats the response code?

0
 

Author Comment

by:lcor
ID: 24109884
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
ID: 24112940
that looks ok, can you post your code

0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:lcor
ID: 24115579
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
ID: 24120414
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
ID: 24120415
why are you including cookies in the request?
0

Featured Post

Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Change in PHP Behavior with Session Write Short Circuit (http://php.net/manual/en/book.session.php#116217) (Winter 2014)** With the release of PHP 5.6 the session handler changed in a way that many think should be considered a bug.  See the note …
A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
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 selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

688 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