c里面使用libcurl库实现发送post并获取post到的内容
来源:互联网 发布:写小说的网站 知乎 编辑:程序博客网 时间:2024/06/06 13:07
以下是c代码的实现:
#include <stdio.h>
#include <stdlib.h>#include <string.h>
#include <curl/curl.h>
#define POSTURL "http://www.xiami.com/member/login"
#define POSTFIELDS "email=myemail@163.com&password=mypassword&autologin=1&submit=登 录&type="
#define FILENAME "curlposttest.log"
size_t write_data(void* buffer,size_t size,size_t nmemb,void *stream)
{
FILE *fptr = (FILE*)stream;
fwrite(buffer,size,nmemb,fptr);
return size*nmemb;
}
int main(int argc,char *argv[])
{
CURL *curl;
CURLcode res;
FILE* fptr;
struct curl_slist *http_header = NULL;
if ((fptr = fopen(FILENAME,"w")) == NULL)
{
fprintf(stderr,"fopen file error:%s\n",FILENAME);
return -1;
}
curl = curl_easy_init();
if (!curl)
{
fprintf(stderr,"curl init failed\n");
return -1;
}
curl_easy_setopt(curl,CURLOPT_URL,POSTURL); //url地址
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,POSTFIELDS); //post参数
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_data); //对返回的数据进行操作的函数地址
curl_easy_setopt(curl,CURLOPT_WRITEDATA,fptr); //这是write_data的第四个参数值
curl_easy_setopt(curl,CURLOPT_POST,1); //设置问非0表示本次操作为post
curl_easy_setopt(curl,CURLOPT_VERBOSE,1); //打印调试信息
curl_easy_setopt(curl,CURLOPT_HEADER,1); //将响应头信息和相应体一起传给write_data
curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1); //设置为非0,响应头信息location
curl_easy_setopt(curl,CURLOPT_COOKIEFILE,"/Users/zhu/CProjects/curlposttest.cookie");
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
switch(res)
{
case CURLE_UNSUPPORTED_PROTOCOL:
fprintf(stderr,"不支持的协议,由URL的头部指定\n");
case CURLE_COULDNT_CONNECT:
fprintf(stderr,"不能连接到remote主机或者代理\n");
case CURLE_HTTP_RETURNED_ERROR:
fprintf(stderr,"http返回错误\n");
case CURLE_READ_ERROR:
fprintf(stderr,"读本地文件错误\n");
default:
fprintf(stderr,"返回值:%d\n",res);
}
return -1;
}
curl_easy_cleanup(curl);
}
0 0
- c里面使用libcurl库实现发送post并获取post到的内容
- c里面使用libcurl库实现发送post并获取post到的内容
- 使用libcurl发送post请求
- http的post方式连接服务器,发送数据到服务端,并获取服务端的数据
- post json数据到springmvc中并获取http请求的内容
- libcurl库的http get和http post使用
- libcurl库的http get和http post使用
- c++ 使用libcurl 发送http get/post请求
- c++ 使用libcurl 发送http get/post请求
- iOS网络篇---使用NSSession 实现post和get发送和接收,并实现字典的发送
- 基于nginx的C模块代码实现获取POST请求参数并回传给客户端
- 使用libcurl提交POST请求
- 使用libcurl提交POST请求
- 使用libcurl提交POST请求
- 使用libcurl提交POST请求
- 使用libcurl提交POST请求
- C/C++使用libcurl库发送http请求(get和post可以用于请求html信息,也可以请求xml和json等串)
- 发送post请求到https的url
- HDU 2023 求平均成绩
- 非常有用的WSDL,有天气预报,股市,交通等等。。。
- 一款免费好用的正则表达式工具:Regex Match Tracer
- 在cocos2d-x中CCTableView控件的使用方法
- hpu 2504 又见GCD
- c里面使用libcurl库实现发送post并获取post到的内容
- 2014-7-25-c#两数相除
- 数据挖掘相关公式的概述
- 数据库系统概论-[04]索引
- 黑马程序员-----OC内存管理(一)
- Best Time to Buy and Sell Stock II
- 伟创OA系统开发设计原则
- ubuntu安装软件的方式
- 最小生成树prim算法实现