默认使用libcurl请求域名的时候,如果域名解析失败会返回一个CURLE_COULDNT_RESOLVE_HOST (6)的错误
但是,如果当你的DNS被运营商劫持后,运营商会返回给你一个错误页面(联通的是wo.com.cn,电信的是114dns),所以这个时候curl获取的结果实际上是这个错误页面,而误认为是请求成功
为了解决这一问题,我尝试获取最终返回的URL地址 与请求的URL地址来进行对比,然后判断
,例如下面的代码
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, urlStr);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,1);
res = curl_easy_perform(curl);
curl_easy_getinfo(curl,CURLINFO_EFFECTIVE_URL,&response_url);
然而事与愿违,最终得到的reponse_url仍然是我请求的url地址而非最终返回的URL地址,所以再次询问各位有没有好的解决方案?
You can first use
nslookup
to resolve a non-existent domain name, such asnslookup xxxxxxxx
. If China Telecom does DNS hijacking, you will resolve an IP address. For example, Shanghai Telecom's fake IP is180.168.41.175
.Install a dnsmasq as your DNS server, write in the dnsmasq configuration file:
Then configure 127.0.0.1 as your DNS server address.
After being configured in this way, dnsmasq will return a domain not exist response to the downstream when the upstream DNS returns
180.168.41.175
, so that you can get accurate error information.