Solved

Tomcat Inputstream Returns -1

Posted on 2009-04-08
6
307 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
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is the cheapest way to learn Websphere MQ? 2 90
wordcount challenge 11 121
servlet example 17 29
Grunt script for Build Process 1 25
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 …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to implement Singleton Design Pattern in Java.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

786 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