曲径通幽论坛

标题: 指定地址解析器(CURLOPT_RESOLVE) [打印本页]

作者: beyes    时间: 2013-7-9 20:19
标题: 指定地址解析器(CURLOPT_RESOLVE)
可以使用 CURLOPT_RESOLVE 选项指定一个域名对应的 IP,而不是通过已知的地址解析器,如 DNS。


测试代码:
[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#include <curl/easy.h>


int main(void)
{
        CURL *curl;
        CURLcode res;


        struct curl_slist *host = NULL;


        host = curl_slist_append(NULL, "groad.net:80:192.168.1.102");


        curl = curl_easy_init();


        if (curl) {
                curl_easy_setopt(curl, CURLOPT_RESOLVE, host);
                curl_easy_setopt(curl, CURLOPT_URL, "http://groad.net/index.html");


                res = curl_easy_perform(curl);


                curl_easy_cleanup(curl);
        }


        curl_slist_free_all(host);


        return (int)res;
}

在上面的代码中显示指定了 groad.net 对应的 IP 为 192.168.1.102,因此程序访问的是 192.168.1.102 这个 IP 的服务器,而不是 DNS 里解析出的公网 IP 。




欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) Powered by Discuz! X3.2