简体   繁体   中英

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

I am crawling a page by libcurl. I need to use specific IP to get page. this ip has been made by the DNS resolver. So I can skip the getaddrinfo in libcurl and cost less time.

I have asked a question How can I use libcurl function "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.

Here's a little sample telling curl example.com is at 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.

( CURLOPT_DNS_LOCAL_IP4 sets "the local IPv4 address that the resolver should bind to" and is thus a completely different functionality)

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