简体   繁体   English

如何使用libcurl获取具有特定服务器IP的页面

[英]How can I use libcurl to get a page with a specific server IP

I am crawling a page by libcurl. 我正在通过libcurl爬行页面。 I need to use specific IP to get page. 我需要使用特定的IP来获取页面。 this ip has been made by the DNS resolver. 该IP由DNS解析器提供。 So I can skip the getaddrinfo in libcurl and cost less time. 因此,我可以跳过libcurl中的getaddrinfo并花费更少的时间。

I have asked a question How can I use libcurl function "curl_easy_setopt(CURL *handle, CURLOPT_DNS_LOCAL_IP4, char *address);" 我问了一个问题, 如何使用libcurl函数“ curl_easy_setopt(CURL * handle,CURLOPT_DNS_LOCAL_IP4,char * address);” but I found this is not what I want. 但是我发现这不是我想要的。

You can "pre-populate" libcurl's DNS cache with CURLOPT_RESOLVE , and then you can keep using the host name in the URL just like normal. 您可以使用CURLOPT_RESOLVE “预填充” libcurl的DNS缓存,然后可以像平常一样继续使用URL中的主机名。

Here's a little sample telling curl example.com is at 127.0.0.1 这是一个告诉curl example.com在127.0.0.1处的小样本

CURL *curl;
struct curl_slist *host = NULL;
host = curl_slist_append(NULL, "example.com:80:127.0.0.1");

curl = curl_easy_init();
if(curl) {
  curl_easy_setopt(curl, CURLOPT_RESOLVE, host);
  curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
  res = curl_easy_perform(curl);

  /* always cleanup */
  curl_easy_cleanup(curl);
}

curl_slist_free_all(host);

Another option is to use the correct IP in the URL and send a custom Host: header that includes the correct host name. 另一种选择是在URL中使用正确的IP,并发送包含正确主机名的自定义Host:标头。

( CURLOPT_DNS_LOCAL_IP4 sets "the local IPv4 address that the resolver should bind to" and is thus a completely different functionality) CURLOPT_DNS_LOCAL_IP4设置“解析程序应绑定到的本地IPv4地址”,因此是完全不同的功能)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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