使用libcurl提交POST请求
最近在学习libcurl,并利用它提交POST请求,可是返回的响应总是无从验证该次POST请求是否成功提交了。
1. 先看下根据firebug提交的一次成功的请求,这里以login我喜欢上的xiami为例,嘻嘻~
1.1 本次POST请求的HTTP交互

1.2 POST

1.3 经server端redirect的GET

2. OK,接下来看下使用libcurl向xiami发送POST请求
2.1 使用libcurl的大概流程
curl_easy_init()
curl_easy_setopt()
curl_easy_perform()
curl_easy_cleanup()
呵呵~超简单的吧,具体的意思这里就不详细说了,参见http://curl.haxx.se/libcurl/c/
2.2 再来看简单的代码
- #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 *userp);
- 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);
- exit(1);
- }
- curl = curl_easy_init();
- curl_easy_setopt(curl, CURLOPT_URL, POSTURL);
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, POSTFIELDS);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, fptr);
- curl_easy_setopt(curl, CURLOPT_POST, 1);
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
- curl_easy_setopt(curl, CURLOPT_HEADER, 1);
- curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
- curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/Users/zhu/CProjects/curlposttest.cookie");
- res = curl_easy_perform(curl);
- curl_easy_cleanup(curl);
- }
- size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) {
- FILE *fptr = (FILE*)userp;
- fwrite(buffer, size, nmemb, fptr);
- }
- #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 *userp);
- 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);
- exit(1);
- }
- curl = curl_easy_init();
- curl_easy_setopt(curl, CURLOPT_URL, POSTURL);
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, POSTFIELDS);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, fptr);
- curl_easy_setopt(curl, CURLOPT_POST, 1);
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
- curl_easy_setopt(curl, CURLOPT_HEADER, 1);
- curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
- curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/Users/zhu/CProjects/curlposttest.cookie");
- res = curl_easy_perform(curl);
- curl_easy_cleanup(curl);
- }
- size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) {
- FILE *fptr = (FILE*)userp;
- fwrite(buffer, size, nmemb, fptr);
- }
2.3 说下这当中的一些操作吧
CURLOPT_URL: URL地址
CURLOPT_POSTFIELDS: POST参数
CURLOPT_WRITEFUNCTION: 对返回的数据进行操作的函数地址
CURLOPT_WRITEDATA: 设置WRITEFUNCTION的第四个参数值
CURLOPT_POST: 设置为非0表示本次操作为POST
CURLOPT_VERBOSE: 设置为非0在执行时打印请求信息
CURLOPT_HEADER: 设置为非0将响应头信息同响应体一起传给WRITEFUNCTION
CURLOPT_FOLLOWLOCATION: 设置为非0,响应头信息Location
CURLOPT_COOKIEFILE: 哈哈,这个实在是太重要了,我之前尝试了好多次没法验证该次POST是否成功的原因就是没有设置这个罗。设置对应的COOKIEFILE路径,该路径文件并不一定需要在物理磁盘上实际存在
2.4 接下来是成功返回的结果哦,呵呵,下面截图当中的zhuzhu可以为证,不好意思,xiami上取了个比较CUO的名字~

使用libcurl提交POST请求的更多相关文章
- php ajax提交post请求出现数组被截断情况的解决方法
一.场景 今天做保存专题商品列表的时候发现,前端明明有2300多条数据,但是实际服务端接受存入数据库才166条 二.解决过程 经过调试发现前端页面提交post请求时数据量是正确的,但到服务端只能接受到 ...
- Android 使用HttpClient方式提交POST请求
final String username = usernameEditText.getText().toString().trim(); final String password = passwr ...
- Android 使用HttpClient方式提交GET请求
public void httpClientGet(View view) { final String username = usernameEditText.getText().toString() ...
- EBS-如何查看非自己提交的请求的结果
http://www.cnblogs.com/quanweiru/p/4692071.html 如何查看非自己提交的请求的结果定位要找的请求SQL举例:SELECT req.request_id, ...
- EBS环境提交新请求默认是"单一请求"
http://blog.csdn.net/samt007/article/details/38304239 用过EBS的请求都知道,提交一个新报表都要点好几个按钮,其中一个很麻烦的就是选择提交新请求的 ...
- libcurl HTTP POST请求向服务器发送json数据【转】
转载:http://blog.csdn.net/dgyanyong/article/details/14166217 转载:http://blog.csdn.net/th_gsb/article/de ...
- 【转】提交http请求之python与curl
提交http请求之python与curl 由于Openstack是python实现wsgi的REST ful架构,在学习和调试的过程中,常常会遇到http请求的提交,于是顺手整理下python和cur ...
- libcurl HTTP POST请求向服务器发送json数据
转载:http://blog.csdn.net/dgyanyong/article/details/14166217 转载:http://blog.csdn.net/th_gsb/article/de ...
- ajax提交post请求出现数组被截断情况的解决方法
一.场景 php post 提交数据时传的数据时数组,没有多数据进行序列化处理.发现传到服务端时,部分数据丢失,查询了资料发现php对参数个数有限制,限制在php配置文件中(max_input_var ...
随机推荐
- 在ie中用滤镜 (filter:progid:DXImageTransform.Microsoft.gradient)会触发overflow:hidden?
1.在ie中用滤镜 (filter:progid:DXImageTransform.Microsoft.gradient)会触发overflow:hidden 在项目中做一个背景层透明内容(菜单)不透 ...
- 自学HTML5第三节(拖放效果)
今天来看看网页上的拖放效果,首先来看看什么是拖放———— 拖放 拖放是一种常见的特性,即抓取对象以后拖到另一个位置. 在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放. 浏览器支持 Inte ...
- zend guard loader
1 .是zendoptimizer的前身, 在php 5.3 (含)之前使用更新到6 ,5.4 之后不再使用.是代码优化的一种,7中opcache 类似功效. 2 .php版本的变量 phpversi ...
- ubuntu下lnmp的安装
适用于ubuntu14.04和源是14.04的ubuntu上安装nginx(我学在线Moodle工作室---注这里安装的是最新版的nginx,并且解决pathinfo问题,特别适用于Moodle安装) ...
- python----脚本文件的头部写法。
#!/usr/bin/python #这里主要是为了指明python脚本解释器的路径. #!coding:utf-8#这个是为了告知python脚本文件解释器,此脚本的字符集. import sys ...
- J2SE知识点摘记(五)
1. 引用数据类型的传递 java用引用代替C++中的指针 fun()方法接收的参数是是Change c1,也就是说说fun()方法接受的是一个对象的引用,所以fun方法中所所做的操作就 ...
- Oracle EBS-SQL (BOM-10):检查有BOM无计划员的数据.sql
select DISTINCT msi.segment1 编码 ,msi.description 描述 ,msi.item_type 物料类型 ,msi.inventory_ ...
- AndroidUI 布局动画-为布局添加动画
除了可以为视图添加动画以外,还可以为视图的布局添加动画: <RelativeLayout xmlns:android="http://schemas.android.com/apk/r ...
- Swift应用开源项目推荐
1. 风靡全球的2048 2014年出现了不少虐心的小游戏,除了名声大噪的Flappy Bird外,最风靡的应该就是2048了.一个看似简单的数字叠加游戏,却让玩的人根本停不下来,朋友圈还一度被晒分数 ...
- collectionView关于点击事件
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)i ...