zoukankan      html  css  js  c++  java
  • libcurl以get方式请求服务器端文件

    static size_t callbackfunction(void *ptr, size_t size, size_t nmemb, void* userdata)
    {
        FILE* stream = (FILE*)userdata;
        if (!stream) {
            printf("!!! No stream ");
            return 0;
        }

        size_t written = fwrite((FILE*)ptr, size, nmemb, stream);
        return written;

    }

    unsigned char* HttpsRequest::requestOnlineImage(const char* url, int& resultLen)
    {
        FILE* fp = fopen("D:\out.png", "wb");
        if (!fp) {

            printf("!!! Failed to create file on the disk ");
            return false;
        }

        unsigned char* result = NULL;
        CURL* mCurl = curl_easy_init();
        if (!mCurl) {
            return NULL;

        }
        //struct curl_slist* mHeaders = NULL;

        //mHeaders = curl_slist_append(mHeaders, "Cache-Control:max-age=0");
        //mHeaders = curl_slist_append(mHeaders, "Connection:keep-alive");
        //mHeaders = curl_slist_append(mHeaders, "*/*");
        //mHeaders = curl_slist_append(mHeaders, "Host:cpi.cn-hangzhou.oss-pub.aliyun-inc.com");
        // mHeaders = curl_slist_append(mHeaders, "User-Agent:com.yunos.map/pc");
        //mHeaders = curl_slist_append(mHeaders, "Content-type:image/png");
        //mHeaders = curl_slist_append(mHeaders, "Response-type:application/octet-stream");
        //mHeaders = curl_slist_append(mHeaders, "Accept-Encoding:gzip, deflate");

        struct MemoryStruct chunk;
        chunk.memory = (unsigned char*)malloc(1);
        chunk.size = 0;
        curl_easy_setopt(mCurl, CURLOPT_URL, url);
        //curl_easy_setopt(mCurl, CURLOPT_HTTPHEADER, mHeaders);
        curl_easy_setopt(mCurl, CURLOPT_WRITEFUNCTION, callbackfunction);
        curl_easy_setopt(mCurl, CURLOPT_WRITEDATA, fp);
        CURLcode mRes = curl_easy_perform(mCurl);

        if (mRes != CURLE_OK){
            fprintf(stderr, "curl_easy_perform() failed: %s ", curl_easy_strerror(mRes));

        }
        else {
            printf("%s ", chunk.memory);
            if (chunk.size > 0) {
                result = (unsigned char*)malloc(chunk.size);
                memcpy(result, chunk.memory, chunk.size);
                resultLen = chunk.size;
            }
        }
        curl_slist_free_all(mHeaders);
        free(chunk.memory); 
        curl_easy_cleanup(mCurl);
        mCurl = NULL;
        return result;

    }

  • 相关阅读:
    CSS媒体查询
    搜索关键词标注红色
    揭秘 | 小白如何0基础0元建站
    细说浏览器输入URL后发生了什么
    js问题总结
    vue elementui如何修改el-table头部样式
    h5开发微信公众号重定向到关注页面没有关注按钮 (微信你个坑)
    下拉展开动画
    html中常用的转义字符总结
    9个设计师常用的高清图库 不敢配图? 这9个免版权图库牢记心中!
  • 原文地址:https://www.cnblogs.com/hustdc/p/6846075.html
Copyright © 2011-2022 走看看