httpclient DNS cache问题解决办法

DNS缓存在操做系统和JDK内部已经实现了吧,通常不须要再由程序介入了。 java

the JVM will cache the dns information for me automatically after the first query缓存

也就是说,httpclient去抓取每一个url时,JVM都会自动cache住 这个url和对应的ip,而且是永远cache住,除非cache住的内容大于 JVM的限制 ,若是未来这个url(域名)更换了ip,httpclient会首先去JVM的cache里取,若是取到了。直接根据这个ip去抓取。url

因此每每某个域名更换了IP,抓取结果都是604错误。spa

解决办法:操作系统

networkaddress.cache.ttl (default: -1)
    Specified in java.security to indicate the caching policy for successful 
    name lookups from the name service. The value is specified as as integer 
    to indicate the number of seconds to cache the successful lookup.

    A value of -1 indicates "cache forever".code

因此只需在java代码里添加:orm

java.security.Security.setProperty("networkaddress.cache.ttl" , "0");
相关文章
相关标签/搜索