URLConnection Low Level Byte Count

I am trying to get the lowest byte count using URLConnection . I already went to count the data passing through two streams: CountingInputStream and CountingOutputStream from Apache Commons IO, but the byte count that I receive is equal to the size of the body that I send + the size of the response body that I receive.

In HttpClient, I had a MeasuringClientConnManager with what I assume is a lower byte byte.

To get the most accurate value, I compare the count of these libraries with these methods using TrafficStats :

public static long getCurrentRx(Context context){ int uid = context.getApplicationInfo().uid; return TrafficStats.getUidRxBytes(uid); } public static long getCurrentTx(Context context){ int uid = context.getApplicationInfo().uid; return TrafficStats.getUidTxBytes(uid); } public static long getCurrentNetworkUsage(Context context){ return getCurrentRx(context) + getCurrentTx(context); } public static long getDiffNetworkUsage(Context context, long previousNetworkUsage){ return getCurrentNetworkUsage(context) - previousNetworkUsage; } 

Using these methods, I got much closer values ​​with HttpClient than URLConnection.

Two questions:

  • How on URLConnection I don’t see headers sent on the byte size indicator of OutputStream? Why do I see there only body size? Where are the headlines? Phone call? Does DNS resolve bytes?

  • Is there a way to measure every byte so that the URLConnection library call is sent over the network interface? The lowest level the better!

+7
java android io apache network-programming
source share
1 answer

URLConnection is designed to abstract the reading (and writing) of headers. The input and output streams of this class represent only the body; There are special procedures for manipulating headers so that they do not need to be written directly.

If you look under the hood in the Sun / Oracle JRE, you will see that their implementation of HttpURLConnection uses its own version of the HTTP client. This client performs a full read and write of all content.

Perhaps you can write your own implementation of URLConnection, which imitates the actions of Sun / Oracle (and Apache), but at the same time it tracks the number of bytes read / written. I argue that this is a bit complicated.

+2
source share

All Articles