您当前的位置: 首页 > 

鱼儿-1226

暂无认证

  • 0浏览

    0关注

    1100博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

使用libcur post请求

鱼儿-1226 发布时间:2021-12-07 17:56:17 ,浏览量:0

#include 
#include 
 
int main(void)
{
  CURL *curl;
  CURLcode res;
 
  /* In windows, this will init the winsock stuff */
  curl_global_init(CURL_GLOBAL_ALL);
 
  /* get a curl handle */
  curl = curl_easy_init();
  if(curl) {
    /* First set the URL that is about to receive our POST. This URL can
       just as well be a https:// URL if that is what should receive the
       data. */
    curl_easy_setopt(curl, CURLOPT_URL, "http://postit.example.com/moo.cgi");
    /* Now specify the POST data */
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl");
 
    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);
    /* Check for errors */
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));
 
    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  curl_global_cleanup();
  return 0;
}

#include 
#include 
//linker options: -lcurl -lcurldll

size_t write_function(void *buff, size_t size, size_t nmemb, FILE *fp){
    //回调函数,下载的数据通过这里写入本地文件
    fwrite(buff, size, nmemb, fp);
    return size*nmemb;
}

int main(int argc, char* argv[]){
    CURL *curl = NULL;
    CURLcode code = 0;
    char url[] = "http://www.lolhelper.cn/rank/rank.php";
    char formdata[] = "daqu=%E7%94%B5%E4%BF%A1%E4%B8%80&nickname=%E4%BC%A0%E5%A5%87%E8%8B%B1%E9%9B%84";
    char filename[] = "c:\\post.html";
    FILE *fp = fopen(filename, "w");


    curl = curl_easy_init();
    if(curl){
        //设置POST协议、URL和FORM_DATA
        curl_easy_setopt(curl, CURLOPT_POST, 1);
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, formdata);
        //设置数据回调
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_function);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        code = curl_easy_perform(curl);

        if(code == CURLE_OK){
            ;;
        }

        curl_easy_cleanup(curl);
    }

    fclose(fp);
    return 0;
}
关注
打赏
1604459285
查看更多评论
立即登录/注册

微信扫码登录

0.0405s