1

For Example

URL yahoo = new URL("http://www.gmail.com/");

So I need to Find the Total internet usage for this request using java..

  • 1
    doubt if it's possible without some os level work – Jason Hu May 16 '15 at 03:41
  • @L0j1k - please read questions properly before voting to close as duplicate. This question is clearly not a duplicate of THAT question. That one is about reading the data. This one is about finding out how much network resources were used while reading the data. – Stephen C May 17 '15 at 00:57
  • Ah, that's weird, I must have clicked the wrong question when I marked it duplicate last night. Funny though that the approval brigade just clicked "duplicate" and it's not even a duplicate of the one I accidentally selected. – L0j1k May 17 '15 at 04:32
  • Yea ... funny about that. – Stephen C May 17 '15 at 07:01

1 Answers1

0

For starters, that statement doesn't generate a "request" and doesn't generate any internet traffic. It is just creating a URL object.

If you were to fetch the content; e.g. like this

    Object obj = new URL("http://www.gmail.com/")
                    .openConnection(). getContent();

then you would generate internet traffic. However, there is no easy way to measure how much traffic is generated from within a Java application.

I guess you could do some clever things in a custom "URLStreamHandler" to measure the number of bytes read and written at the socket stream level. But that doesn't tell you "total internet usage"; i.e. how many packets were read and written and how big they were. And it doesn't include the network traffic (potentially) generated by the DNS lookup.

If you want to measure real ("total") network traffic accurately, you are better of doing it at the OS level, or at the network level.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216