Linux下使用CURL获取Rest接口数据
来源:互联网 发布:手机铃声制作软件 mac 编辑:程序博客网 时间:2024/05/19 16:02
/*
gcc -o test -Wall test.c -lcurl
*/
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/select.h>
#include <curl/curl.h>
#include <net/if.h>
#include <sys/ioctl.h>
#define SIZE 1024
#define SERVER_PORT 80
long getlocalhostip(){
int MAXINTERFACES=16;
long ip;
int fd, intrface, retn = 0;
struct ifreq buf[MAXINTERFACES]; ///if.h
struct ifconf ifc; ///if.h
ip = -1;
if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) >= 0) //socket.h
{
ifc.ifc_len = sizeof buf;
ifc.ifc_buf = (caddr_t) buf;
if (!ioctl (fd, SIOCGIFCONF, (char *) &ifc)) //ioctl.h
{
intrface = ifc.ifc_len / sizeof (struct ifreq);
while (intrface-- > 0)
{
if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[intrface])))
{
ip=inet_addr( inet_ntoa( ((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr) );//types
break;
}
}
}
close (fd);
}
return ip;
}
union ipu{
long ip;
unsigned char ipchar[4];
};
int main()
{
union ipu iptest;
iptest.ip = getlocalhostip();
//set your url
char ipstr[100] = "http://172.18.200.88/mod/experiment/receivevm/api.php?ip=";
char ipstritem[3];
char joinstr[] = ".";
int i=0;
for(; i<4; i++){
if(i!=0)
strcat(ipstr, joinstr);
sprintf(ipstritem, "%d", iptest.ipchar[i]);
strcat(ipstr, ipstritem);
}
//printf("%s \n", ipstr);
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl!=NULL)
{
curl_easy_setopt(curl, CURLOPT_URL, ipstr);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
return 0;
gcc -o test -Wall test.c -lcurl
*/
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/select.h>
#include <curl/curl.h>
#include <net/if.h>
#include <sys/ioctl.h>
#define SIZE 1024
#define SERVER_PORT 80
long getlocalhostip(){
int MAXINTERFACES=16;
long ip;
int fd, intrface, retn = 0;
struct ifreq buf[MAXINTERFACES]; ///if.h
struct ifconf ifc; ///if.h
ip = -1;
if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) >= 0) //socket.h
{
ifc.ifc_len = sizeof buf;
ifc.ifc_buf = (caddr_t) buf;
if (!ioctl (fd, SIOCGIFCONF, (char *) &ifc)) //ioctl.h
{
intrface = ifc.ifc_len / sizeof (struct ifreq);
while (intrface-- > 0)
{
if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[intrface])))
{
ip=inet_addr( inet_ntoa( ((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr) );//types
break;
}
}
}
close (fd);
}
return ip;
}
union ipu{
long ip;
unsigned char ipchar[4];
};
int main()
{
union ipu iptest;
iptest.ip = getlocalhostip();
//set your url
char ipstr[100] = "http://172.18.200.88/mod/experiment/receivevm/api.php?ip=";
char ipstritem[3];
char joinstr[] = ".";
int i=0;
for(; i<4; i++){
if(i!=0)
strcat(ipstr, joinstr);
sprintf(ipstritem, "%d", iptest.ipchar[i]);
strcat(ipstr, ipstritem);
}
//printf("%s \n", ipstr);
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl!=NULL)
{
curl_easy_setopt(curl, CURLOPT_URL, ipstr);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
return 0;
}
CURL真的很好要,不过windows下使用wininet也是比较方便的。如果你需要代码统一,而且方便以后维护的话,可以都使用CRUL。
- Linux下使用CURL获取Rest接口数据
- 使用cURL调试REST接口
- curl获取接口数据
- Linux下使用curl获取公网IP
- 使用curl获取数据
- 使用 REST 接口获取订阅下虚拟机信息
- Linux下使用curl
- Linux下使用curl
- Linux下使用curl
- Linux下使用curl
- php curl请求接口并获取数据
- php curl请求接口并获取数据
- php curl请求接口并获取数据
- 通过curl访问rest接口
- C#如何使用REST接口读写数据
- Linux下curl的使用
- linux使用curl进行接口测试
- PHP下使用CURL方式POST数据至API接口的方法
- Android 开发环境搭建——(Android开发笔记,WIN7)
- android-----开发环境搭建(JNI+android4.2)
- android常用代码片段
- 手把手教学:详解HTML5移动开发框架PhoneJS
- 浅析:RAID技术重点知识以及RAID特点
- Linux下使用CURL获取Rest接口数据
- 如何将c程序执行文件打包入APK中,两种方法
- C++ fstream和getline的用法
- JDBC注册驱动程序三种方式
- 自增字段 auto_commit的研究分析
- windows远程Putty+Xming使用方法
- 大数据:“人工特征工程+线性模型”的尽头
- nginx源码学习(二) 内存池结构 ngx_pool_t
- 集合映射配置