• Advertisement
Sign in to follow this  

J2ME - HTTPConnection (get total download bytes?)

This topic is 4388 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, how does one get the total download size of an http connection? i tried getLength() of HttpConnection class, but it only returns the content-length part of the HTTP response. I need to have a traffic counter in my app, whose download value is the same as that reported by a phone's GPRS counter. please help. thanks.

Share this post


Link to post
Share on other sites
Advertisement
You mean you want to include all the headers in your count, right?

If so, what you would need to do is use a StreamConnection and do the counting (and the header parsing too because your not using an HttpConnection) manually.

shmoove

Share this post


Link to post
Share on other sites
hi, yup, i'd like to count the header size also.

let's say i use the streamconnection class, how do i grab only the content (no header) data?

thanks.

Share this post


Link to post
Share on other sites
The body (or content) starts immediately after you find the "\r\n\r\n" character sequence. So you either put everything in a buffer and then search for the first occurrence of those characters or you can test the characters as you are reading them in.

Http specs.

shmoove

Share this post


Link to post
Share on other sites
Why wouldn't this solution work with MIDP 1.0 (CLDC 1.0 if you want to be more precise)?

shmoove

Share this post


Link to post
Share on other sites
Quote:
Original post by shmoove
You mean you want to include all the headers in your count, right?

If so, what you would need to do is use a StreamConnection and do the counting (and the header parsing too because your not using an HttpConnection) manually.

shmoove



Hi,

I tried using the StreamConnection class to replace HTTPConnection. But it always sends an HTTP GET request...i need to use HTTP POST...any ideas how to go about it?

thanks.

Share this post


Link to post
Share on other sites
That just made me notice I forgot a very important part. When you do Connector.open("http://whatever.com"), the Connector class will "use HttpConnection" (ie, will perform all the actions it does for opening an HttpConnection) no matter what class you are using to reference that connection.

So in order to actually get the low level control you need to parse the headers on your own you need to open it as a socket connection (ie, Connector.open("socket://whatever.com:80")). Then you get the OutputStream from that connection and manually build and send (by writing to the OutputStream) the http request (according to the rules I linked above) in any way you like it.

A GET request would look like this:

GET /index.html?userid=joe&password=guessme HTTP/1.1
Host: www.mysite.com
User-Agent: MIDP 1.0/CLDC 1.0


A POST request would look like this:

POST /login.jsp HTTP/1.1
Host: www.mysite.com
User-Agent: MIDP 1.0/CLDC 1.0
Content-Length: 27
Content-Type: application/x-www-form-urlencoded

userid=joe&password=guessme


The problem this raises is that in fact many of the older phones don't support opening socket connections. For these phones the only alternative I can think of is trying to estimate the size of the headers by getting all of them with a loop of HttpConnection.getHeaderField(int i) calls so you get all the headers sent with the response.

I'm sorry if I led you down a wild goose chase.

shmoove

Share this post


Link to post
Share on other sites
that is ok, i really appreciate the help.

just a question, how can i use "socket://something:80" if my link has a directory?

i tried:

"socket://something/wv:80" and "socket://something:80/wv"

and both didn't work.

thanks!

Share this post


Link to post
Share on other sites
The link to the directory would be part of the http request you build manually. You open a connection to "socket://something:80", and in the http request you ask for the subdirectory you want (notice that right after "POST" I ask for "vw/login.jsp"):

POST vw/login.jsp HTTP/1.1

Host: something

User-Agent: MIDP 1.0/CLDC 1.0

Content-Length: 27

Content-Type: application/x-www-form-urlencoded



userid=joe&password=guessme


shmoove

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement