首页 > 其他分享 >libcurl 发送HTTP请求时获取目标IP

libcurl 发送HTTP请求时获取目标IP

时间:2022-11-15 23:00:24浏览次数:83  
标签:HTTP libcurl IP CURLINFO easy ip curl pointer

#include "curl/curl.h"


int main() {
    char* ip;
    CURL* curl = curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_URL, "https://baidu.com");
    CURLcode res = curl_easy_perform(curl);
    if(res == CURLE_OK && !curl_easy_getinfo(curl, CURLINFO_PRIMARY_IP, &ip) && ip) {
        printf("IP: %s\n", ip);
    }
    int response_code;
    if (!curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code)){
       printf("Status Code: %d\n", response_code);
    }
    curl_easy_cleanup(curl);
    return 0;
}

输出:

<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>bfe/1.0.8.18</center>
</body>
</html>
IP: 39.156.66.10
Status Code: 302

可以通过 CURLINFO_PRIMARY_IP 获取curl最近一次的连接IP

https://curl.se/libcurl/c/CURLINFO_PRIMARY_IP.html

CURLINFO_PRIMARY_IP explained

Pass a pointer to a char pointer to receive the pointer to a null-terminated string holding the IP address of the most recent connection done with this curl handle. This string may be IPv6 when that is enabled. Note that you get a pointer to a memory area that will be re-used at next request so you need to copy the string if you want to keep the information.

The ip pointer will be NULL or pointing to private memory you MUST NOT free - it gets freed when you call curl_easy_cleanup on the corresponding CURL handle.

Availability

Added in 7.19.0

 

标签:HTTP,libcurl,IP,CURLINFO,easy,ip,curl,pointer
From: https://www.cnblogs.com/ed557/p/16894375.html

相关文章

  • JavaScript基础知识——数据类型
    数据类型在JavaScript中有8中基本数据类型,7种原始类型和1种引用类型。可以将任何类型的值存入变量。例如,一个变量可以在前一刻是个字符串,下一个就存储一个数字。如:letm......
  • golang发送HTTP请求时获取目标IP (server ip)
    golangnet/http库在发送http请求时会通过调用net下的Dialer建立TCP连接,net.Dialer会在发起连接前执行通过ControlContext字段传入的一个函数,我们可以通过这个函数获取i......
  • 后端程序员必会的前端知识-02:JavaScript
    第二章.Javascript它是一种脚本语言,可以用来更改页面内容,控制多媒体,制作图像、动画等等例子修改页面内容js代码位置<script> //js代码</script>引入js脚......
  • JavaScript基础知识
    变量变量是数据的命名存储,我们可以用变量来保存商品、访客和其他信息。在JavaScript中创建一个变量,需要用到关键字let。例如:letmessage="hello";//将字符串hello保......
  • 有趣的 Go HttpClient 超时机制
    hello,大家好呀,我是既写Java又写Go的小楼,在写Go的过程中经常对比这两种语言的特性,踩了不少坑,也发现了不少有意思的地方,今天就来聊聊Go自带的HttpClient的超时机制......
  • HCIP-ICT实战进阶05-路由策略与策略路由
    HCIP-ICT实战进阶05-路由策略与策略路由0前言什么是路由策略?基于报文的目的IP地址进行路由表查找,之后转发数据;针对控制平面,为路由协议和路由表服务,针对路由......
  • http与https的区别、TCP三次握手与四次挥手
    ·http定义:http,超文本传输协议,是一个基于请求与响应的,无状态的,应用层协议,常基于TCP/IP协议进行传输数据,是互联网上最为广泛运用的传输协议,所有的www文件均需遵守该标准。......
  • iPhone所有手机型号屏幕尺寸
    1、表格整理手机型号尺寸物理点宽长比例像素点倍数状态栏高度底部安全距离导航栏高度tabbar高度iPhone4/4S3.5英寸320x4800.667640x960@2x20-4......
  • 常用的JavaScript代码技巧 (一)字符串、数字
    一、字符串类1.比较时间consttime1="2022-03-0510:00:00";consttime2="2022-03-0510:00:01";constovertime=time1<time2;//overtime=>true2.货币格式......
  • 常用的JavaScript代码技巧 (二)布尔、数组
    一、布尔1.基础操作consta=true&&false;//falseconstb=true||false;//trueconstc=!0;//true2.确定数据类型不判断的类型:undefined,null,stri......