当前位置:   article > 正文

安装libcurl库访问一下百度_windows环境gcc安装liburl

windows环境gcc安装liburl

一、下载libcurl库
首先在github上下载libcurl库
网址:https://github.com/curl/curl/releases/tag/curl-7_71_1
二、放入虚拟机里面
在虚拟机上新建一个文档
在这里插入图片描述
接下来解压:tar -vxf curl-7.71.1.tar.bz2
三、需要安装libcurl库
1.读README
在这里插入图片描述
2.看docs里面的INSTALL
在这里插入图片描述
(1)./configure是配置指令,默认安装路径到usr/local/下
(2)./configure --prefix=$PWD/_install 这是在配置自己指定路径,表示安装在当前路径的_install文件下

四、访问一下百度
1.访问程序
由于C语言中没有bool类型,所以我们需要进行一点修改

#include <stdio.h>
#include <curl/curl.h>
typedef unsigned int bool
#define true 1
#define false 0
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");
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

整个流程:首先main函数入口进去,调用getUrl函数,在/tmp/get.html中开辟一个字符串地址空间,准备存放百度连接的源码,然后是fp = fopen()打开filename就是打开这个地址空间,没有就创建一个,然后curl_easy_init()得到easyinterface型的指针也就是curl句柄,后面都是根据这个curl句柄操作,然后调用curl_easy_setopt()设置协议头和百度连接,然后调用curl_easy_setopt()实现回调函数完成特定任务,CURLOPT_WRITEDATA的作用就是请求的链接内容返回到回调函数里面,存到fp指向的地址里面,也就是/tmp/get.html中,最后执行和释放内存。
2.编译
直接gcc demo1.c的话会出现以下错误
在这里插入图片描述
说明需要链库:gcc demo1.c -I ./curl-7.71.1/_install/include/ -L ./curl-7.71.1/_install/lib/ -lcurl
分别是链头文件和链库
最后会编译成功:
在这里插入图片描述
打开/tmp/get.html验证一下
在这里插入图片描述
这些就是百度官方网页源代码

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/744110
推荐阅读
相关标签
  

闽ICP备14008679号