Linux下使用CURL获取Rest接口数据
2013-09-13 15:10
627 查看
/*
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。
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。
相关文章推荐
- php使用CURL模拟GET与POST向微信接口提交及获取数据的方法
- PHP下使用CURL方式POST数据至API接口的代码
- 调用REST接口获取数据
- Linux中Curl命令couldn't connect to host解决方案 php操作Curl(http,https)无法获取远程数据解决方案
- php使用curl通过代理获取数据的实现方法
- 通过wireshark获取应用接口并使用爬虫爬取网站数据(三)
- 使用postman请求springmvc接口时,request获取不到数据
- PHP中使用CURL模拟登录并获取数据实例
- 【动态代理】使用动态代理解析注解原数据,获取接口信息
- Linux下使用Curl调用Java的WebService接口
- PHP下使用CURL方式POST数据至API接口的代码
- PHP下使用CURL方式POST数据至API接口的方法
- elasticsearch使用restclient接口插入数据
- Linux下使用命令获取redis数据脚本
- C#如何使用REST接口读写数据
- Thinkphp3.2.3框架下封装公共的函数,例如封装CURL函数来获取接口数据
- linux下使用C获取mp3 ID3数据时的一些注意事项(v1,v2.3,v2.4)
- php file_get_contents和curl请求接口并获取数据
- 不用file_get_contents,用curl时。接口调用数据获取
- C#如何使用REST接口读写数据