java监听网络连接,如何使用Java监控我的网络连接?

Is there any sample code that can report the url that my PC is connecting with Java?

While I use my browser to connect to different sites and watch video online, it should capture the urls. Is it doable in Java?

I don't want detailed traffic, just record the urls.

解决方案

The fastest way is probably going to be to capture the output of the command-line tshark program (for at least windows and linux). This works on my linux box:

sudo tshark -f 'port 80' -R 'http' -V | grep -A 1 '^Hypertext Transfer Protocol'

And produces output like:

Running as user "root" and group "root". This could be dangerous.

Capturing on eth0

Hypertext Transfer Protocol

GET /questions/4494294/r-gplots-barplots-how-to-fix-bar-width-independent-of-paper-setting HTTP/1.1\r\n

--

Hypertext Transfer Protocol

HTTP/1.1 200 OK\r\n

--

Hypertext Transfer Protocol

GET /posts/4494294/ivc/086e HTTP/1.1\r\n

--

Hypertext Transfer Protocol

HTTP/1.1 204 No Content\r\n

--

Hypertext Transfer Protocol

[truncated] GET /__utm.gif?utmwv=4.8.6&utmn=20455052&utmhn=stackoverflow.com&utmcs=UTF-8&utmsr=1600x1200&utmsc=24-bit&utmul=en-us&utmje=1&utmfl=10.1%20r102&utmdt=graph%20-%20R%3A%20gplots%2C%20barplots%3A%20how%20to%20fix%20bar%20width%20i

--

Hypertext Transfer Protocol

HTTP/1.1 200 OK\r\n

--

You should get something similar for Windows. Experiment. How you get the output of tshark into java is up to you.

If you want an all-java solution, how about http://jnetpcap.com/ ?

You're going to have to write (or use a library that provides) some platform specific JNI code.

libpcap/WinPcap that jNetPcap uses provides this reliably on at least Windows and Linux from my experience. I have experience with libpcap/WinPcap but not with jNetPcap.

tshark also uses libpcap/WinPcap actually.

Either way, you're going to see very many false positives. Opening a HTML page requires getting many, many images, style sheets, javascript libraries etc. The snippet above is from opening a stackoverflow.com page.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值