一、http协议详解

https://www.cnblogs.com/xietianjiao/p/13255099.html

二、https协议详解

https://www.cnblogs.com/xietianjiao/p/13255408.html

三、libcurl库详解

https://www.cnblogs.com/xietianjiao/p/13260021.html

四、基于libcurl库访问百度

#include <stdio.h>
#include <curl/curl.h>
#include <string.h>

#define true 1
#define false 0
typedef unsigned int bool;


size_t readData( void *ptr, size_t size, size_t nmemb, void *stream)
{
        char buf[10240]={'\0'};
        strncpy(buf,ptr,10240);
        printf("===============================readData===========================\n");
        printf("buf=%s\n",buf);
}

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_WRITEFUNCTION,readData); //将返回的http头输出到fp指向的文件
//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");
}

五、基于翔云OCR实现人脸识别

#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

#define true 1
#define false 0
typedef unsigned int bool;
char buf[1024]={'\0'};

size_t readData( void *ptr, size_t size, size_t nmemb, void *stream)
{
        strncpy(buf,ptr,1024);
//      printf("===============================readData===========================\n");
//      printf("buf=%s\n",buf);
}
char* getBase64Jpg(char* filePath)
{
        char* bufjpg;
        char cmd[128]={'\0'};
        sprintf(cmd,"base64 %s > temFile",filePath);
        system(cmd);//转换为base64图片流
        int fd=open("./temFile",O_RDWR);//打开文件
        int fileLen=lseek(fd,0,SEEK_END);//计算文件大小
        lseek(fd,0,SEEK_SET);//使光标回到初始位置
        bufjpg=(char*)malloc(fileLen);//定义指针并申请空间
        memset(bufjpg,'\0',fileLen+2);//文件内容清零
        read(fd,bufjpg,fileLen+2);//读取文件内容
        //printf("file=%s\n",buf);      
 close(fd);
        system("rm -f temFile");//删除临时文件
        return bufjpg;
}
bool postUrl()
{
        CURL *curl;
        CURLcode res;
        char *poString;
        char *img1;//上传的文件(图片的base64流) 
        char *img2;//上传的文件(图片的base64流) 
        char *key="C4Ud2igZbmrecRo3LKam7e";//用户ocrKey
        char *secret="49c2c4baf53647fdb908c4eb7fbe6996";//用户ocrSecrert
        int  typeId=21;//识别类型:21
        char *format="xml";//返回格式(xml或者json),如果format为空,则默认返回xml 

        char* bufjpg1=getBase64Jpg("./1.jpg");
        char* bufjpg2=getBase64Jpg("./2.jpg");

        int len=strlen(key)+strlen(secret)+strlen(bufjpg1)+strlen(bufjpg2)+124;
        poString=(char*)malloc(len);//申请空间
        memset(poString,'\0',len);//文件内容清零
        //int sprintf(char *string, char *format [,argument,...]);把格式化的数据写入某个字符串缓冲区。
        sprintf(poString,"&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",
                         bufjpg1,bufjpg2,key,secret,21,format);//拼接字符串
        curl = curl_easy_init();
        if (curl)
        {
                curl_easy_setopt(curl, CURLOPT_POSTFIELDS, poString);    // 指定post内容
                curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do");   // 指定url
                curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,readData); //将返回的http头输出到fp指向的文件
                res = curl_easy_perform(curl);
 printf("OK:%d\n",res);
                if(strstr(buf,"是") != NULL)
                {
                        printf("this is a same person\n");
                }
                else
                {
                        printf("different person\n");
                }
                curl_easy_cleanup(curl);
        }
        return true;
}
int main(void)
{
        postUrl();
}

 

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐