HttpConnection - javax.microedition returning -1 for the getLength () method

I am trying to program a very simple mobile application (J2ME) in java. The idea is to access the website through a URL and read the contents of the website into a clipboard.

Here is the problem. Does this work fine for some URLs, but not for others? The example below (Wikipedia) works fine. But take "http://java.com/en/about/" as an example, and "HttpConnection hc" returns -1 for getLenght (), so there is no content to read into the buffer?

Here is my code:

String url = "http://en.wikipedia.org/wiki/RSS"; //Sets up HttpConnection and InputStream using the URL variable HttpConnection hc = null; InputStream is = null; try { hc = (HttpConnection) Connector.open(url); is = hc.openInputStream(); } catch (IOException ie) { System.out.println(ie.getMessage()); } //Reader object created to read input from InputStream Reader rdr = new InputStreamReader(is); //Variable "content" will store HTML code String content = ""; //Get the lenght of the data to set the buffer sizes int len = (int) hc.getLength(); 

Any ideas? let me know if i missed something!

For information only, I'm using Netbeans 6.9.1

The library for HttpConnection is "javax.microedition.io.HttpConnection;" + "import javax.microedition.io.Connector;"

+1
source share
1 answer

HTTP response from java.com

 HTTP/1.1 200 OK Server: Sun-Java-System-Web-Server/7.0 Date: Wed, 23 Feb 2011 11:07:44 GMT Content-Type: text/html;charset=UTF-8 Set-Cookie: JSESSIONID=B62F3DFB233BB2806018EC721F6C3FD7; Path=/ Content-Encoding: gzip Vary: accept-encoding Transfer-Encoding: chunked 

HTTP response from wikipedia

 HTTP/1.0 200 OK Date: Wed, 23 Feb 2011 10:18:56 GMT Server: Apache Cache-Control: private, s-maxage=0, max-age=0, must-revalidate Content-Language: en Vary: Accept-Encoding,Cookie Last-Modified: Fri, 18 Feb 2011 00:23:59 GMT Content-Encoding: gzip Content-Length: 24905 Content-Type: text/html; charset=UTF-8 Age: 2984 X-Cache: HIT from sq61.wikimedia.org, MISS from sq38.wikimedia.org X-Cache-Lookup: HIT from sq61.wikimedia.org:3128, MISS from sq38.wikimedia.org:80 Connection: keep-alive 

As you can see, the http response http://java.com/en/about/ does not contain the Content-Length header, the content is marked.

So getLength () returns -1.

+2
source

All Articles