• 11
• 9
• 12
• 9
• 11

# J2ME - HTTPConnection (get total download bytes?)

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

## 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 on other sites
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 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 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 on other sites
anyone know of a solution that'll work for MIDP1? :(

##### 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 on other sites
oh, you're right...sorry 'bout that...:)

##### Share on other sites
Quote:
 Original post by shmooveYou 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 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.1Host: www.mysite.comUser-Agent: MIDP 1.0/CLDC 1.0

A POST request would look like this:
POST /login.jsp HTTP/1.1Host: www.mysite.comUser-Agent: MIDP 1.0/CLDC 1.0Content-Length: 27Content-Type: application/x-www-form-urlencodeduserid=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 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!