简体   繁体   中英

How to change the ip address for a domain from the java program itself?

we manually do the host entries to redirect a URL to a particular IP, Is there any way to do that in the automation script itself(in JAVA)? So it points to a particular IP without every time changing the host entries manually from the system hosts file.

You can use BrowserMob-Proxy to fake IP addresses on the fly. You need to add your custom domain resolver like shown here:

browserMobProxy.setHostNameResolver(new NativeResolver(){
    @Override
    public Collection<InetAddress> resolve(String originalHost) {
        if("webelement.click".equals(originalHost)){
            try {
                return Arrays
                        .asList(
                                new InetAddress[]{
                                        InetAddress.getByName("127.0.0.1")
                                });
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }
        return super.resolve(originalHost);
    }
});

In this example you override the IP for one particular domain name. In this post you can find more insight on the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM