Http协议之libcurl实现
http协议之详解
http协议之https
http协议之libcurl
参考博文https://www.cnblogs.com/xietianjiao/p/13260021.html
libcurl库下载
https://github.com/curl/curl/releases/tag/curl-7_71_1
openssl安装
https://blog.csdn.net/weixin_38184741/article/details/86554438
wget https://www.openssl.org/source/openssl-1.1.1a.tar.gz
tar -vxf openssl-1.1.1a.tar.gz
cd openssl-1.1.1a/
./config
make
sudo make install
./config
在目录中运行
会生成一个_install
–host = arm-linux
./configure --prefix=$PWD/_install --with-ssl
再执行
make
最后install
sudo make install
测试代码
#include <stdio.h>
#include <curl/curl.h>
#define true 1
#define false 0
typedef unsigned int bool;
bool getUrl(char *filename)
{
CURL *curl;
CURLcode res;
FILE *fp;
if ((fp = fopen(filename, "w")) == NULL) // 返回结果用文件存储
return false;
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: Agent-007");
curl = curl_easy_init(); // 初始化
if (curl)
{
//curl_easy_setopt(curl, CURLOPT_PROXY, "10.99.60.201:8080");// 代理
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);// 改协议头
curl_easy_setopt(curl, CURLOPT_URL,"http://www.baidu.com");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); //将返回的http头输出到fp指向的文件
curl_easy_setopt(curl, CURLOPT_HEADERDATA, fp); //将返回的html主体数据输出到fp指向的文件
res = curl_easy_perform(curl); // 执行
if (res != 0) {
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
fclose(fp);
return true;
}
}
bool postUrl(char *filename)
{
CURL *curl;
CURLcode res;
FILE *fp;
if ((fp = fopen(filename, "w")) == NULL)
return false;
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt"); // 指定cookie文件
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "&logintype=uid&u=xieyan&psw=xxx86"); // 指定post内容
//curl_easy_setopt(curl, CURLOPT_PROXY, "10.99.60.201:8080");
curl_easy_setopt(curl, CURLOPT_URL, " http://mail.sina.com.cn/cgi-bin/login.cgi "); // 指定url
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
fclose(fp);
return true;
}
int main(void)
{
getUrl("/tmp/get.html");
postUrl("/tmp/post.html");
}
export LD_LIBRARY_PATH=/home/other/test/lib:$LD_LIBRARY_PATH
安装文件的lib库链一下
gcc编译
gcc baidu.c -I /home/CLC/curl-7.71.1/_install/include/ -L /home/CLC/curl-7.71.1/_install/lib/ -lcurl
文件名 -I是头文件#include <curl/curl.h>底下 -L是 -lcurl库的位置
此时会在/tmp下生成一个get.html