How to detect public IP address (of router/gateway) change in .Net?
I am aware of the NetworkChange.NetworkAddressChanged
event, but that is only for the local network interfaces.
What is the best way to detect the public IP开发者_如何学运维 address of the gateway has changed? Presumably one has to do it by polling an external reliable address, such a google.com...?
Here's one by Akamai: http://whatismyip.akamai.com/
If the router supports it, you could use SNMP to query for its public IP address (or, at least, when I tried to do this I had to query for all IP addresses and filter out the private address(es)). You'll need to identify the OIDs (object IDs) that correspond to the IP address values, which I believe should be the same across all routers. Poll the router at regular intervals and compare the previous result to the current result. Alternatively, set up an SNMP Trap so the router will notify your application of a change in IP address. It might take more work to implement than simply processing the HTML from a "what is my IP" website, but this will be more efficient, possibly more reliable, won't generate any WAN traffic, and you won't have to rely on a website you don't control that could break your application by blocking you or changing the structure of their HTML.
#SNMP is an open source SNMP library for .NET.
Using an external API is the only reliable method. Most of them who reveals your IP address will block a machine made request and ask for a CAPTCHA input. Here is a good one which allows machines to make request:
http://ip.nefsc.noaa.gov
精彩评论