最近在学习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 再来看简单的代码

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <curl/curl.h>
  5. #define POSTURL "http://www.xiami.com/member/login"
  6. #define POSTFIELDS "email=myemail@163.com&password=mypassword&autologin=1&submit=登 录&type="
  7. #define FILENAME "curlposttest.log"
  8. size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp);
  9. int main(int argc, char *argv[]) {
  10. CURL *curl;
  11. CURLcode res;
  12. FILE *fptr;
  13. struct curl_slist *http_header = NULL;
  14. if ((fptr = fopen(FILENAME, "w")) == NULL) {
  15. fprintf(stderr, "fopen file error: %s\n", FILENAME);
  16. exit(1);
  17. }
  18. curl = curl_easy_init();
  19. curl_easy_setopt(curl, CURLOPT_URL, POSTURL);
  20. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, POSTFIELDS);
  21. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  22. curl_easy_setopt(curl, CURLOPT_WRITEDATA, fptr);
  23. curl_easy_setopt(curl, CURLOPT_POST, 1);
  24. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  25. curl_easy_setopt(curl, CURLOPT_HEADER, 1);
  26. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  27. curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/Users/zhu/CProjects/curlposttest.cookie");
  28. res = curl_easy_perform(curl);
  29. curl_easy_cleanup(curl);
  30. }
  31. size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) {
  32. FILE *fptr = (FILE*)userp;
  33. fwrite(buffer, size, nmemb, fptr);
  34. }
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <curl/curl.h>
  5. #define POSTURL "http://www.xiami.com/member/login"
  6. #define POSTFIELDS "email=myemail@163.com&password=mypassword&autologin=1&submit=登 录&type="
  7. #define FILENAME "curlposttest.log"
  8. size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp);
  9. int main(int argc, char *argv[]) {
  10. CURL *curl;
  11. CURLcode res;
  12. FILE *fptr;
  13. struct curl_slist *http_header = NULL;
  14. if ((fptr = fopen(FILENAME, "w")) == NULL) {
  15. fprintf(stderr, "fopen file error: %s\n", FILENAME);
  16. exit(1);
  17. }
  18. curl = curl_easy_init();
  19. curl_easy_setopt(curl, CURLOPT_URL, POSTURL);
  20. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, POSTFIELDS);
  21. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  22. curl_easy_setopt(curl, CURLOPT_WRITEDATA, fptr);
  23. curl_easy_setopt(curl, CURLOPT_POST, 1);
  24. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  25. curl_easy_setopt(curl, CURLOPT_HEADER, 1);
  26. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  27. curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/Users/zhu/CProjects/curlposttest.cookie");
  28. res = curl_easy_perform(curl);
  29. curl_easy_cleanup(curl);
  30. }
  31. size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) {
  32. FILE *fptr = (FILE*)userp;
  33. fwrite(buffer, size, nmemb, fptr);
  34. }

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请求的更多相关文章

  1. php ajax提交post请求出现数组被截断情况的解决方法

    一.场景 今天做保存专题商品列表的时候发现,前端明明有2300多条数据,但是实际服务端接受存入数据库才166条 二.解决过程 经过调试发现前端页面提交post请求时数据量是正确的,但到服务端只能接受到 ...

  2. Android 使用HttpClient方式提交POST请求

    final String username = usernameEditText.getText().toString().trim(); final String password = passwr ...

  3. Android 使用HttpClient方式提交GET请求

    public void httpClientGet(View view) { final String username = usernameEditText.getText().toString() ...

  4. EBS-如何查看非自己提交的请求的结果

    http://www.cnblogs.com/quanweiru/p/4692071.html 如何查看非自己提交的请求的结果定位要找的请求SQL举例:SELECT req.request_id,   ...

  5. EBS环境提交新请求默认是"单一请求"

    http://blog.csdn.net/samt007/article/details/38304239 用过EBS的请求都知道,提交一个新报表都要点好几个按钮,其中一个很麻烦的就是选择提交新请求的 ...

  6. libcurl HTTP POST请求向服务器发送json数据【转】

    转载:http://blog.csdn.net/dgyanyong/article/details/14166217 转载:http://blog.csdn.net/th_gsb/article/de ...

  7. 【转】提交http请求之python与curl

    提交http请求之python与curl 由于Openstack是python实现wsgi的REST ful架构,在学习和调试的过程中,常常会遇到http请求的提交,于是顺手整理下python和cur ...

  8. libcurl HTTP POST请求向服务器发送json数据

    转载:http://blog.csdn.net/dgyanyong/article/details/14166217 转载:http://blog.csdn.net/th_gsb/article/de ...

  9. ajax提交post请求出现数组被截断情况的解决方法

    一.场景 php post 提交数据时传的数据时数组,没有多数据进行序列化处理.发现传到服务端时,部分数据丢失,查询了资料发现php对参数个数有限制,限制在php配置文件中(max_input_var ...

随机推荐

  1. javascript的调试

    1. 关于javascript的调试,最好将浏览器的脚本通知打开. 2. 最好设置为每次访问页面时检查.

  2. 关于Application Cache

    http://blog.csdn.net/fwwdn/article/details/8082433 http://www.cnblogs.com/blackbird/archive/2012/06/ ...

  3. 点击超链接执行js代码实现确认操作

    如题,本次是要实现点击超链接实现执行js代码,并确认是否删除数据库数据,采用php. 首先链接数据库,查询数据库数据: 1: <?php 2: $dbms='mysql'; //数据库类型 ,对 ...

  4. 给新建的Cocos2d-x 3.X的Win32工程添加CocoStudio库

    1.我们在VS中找到"解决方案资源管理器", 在解决方案"HelloCocos"上点击右键, 选择添加现有项目. 在弹出的对话框中选择************* ...

  5. 驳Linux不娱乐 堪比Win平台中十款播放器

    播放器在我们日常生活中扮演着非常重要的角色,在Windows操作系统中,播放器被应用的非常广泛,不但我们可以听音乐,甚至还可以听广播,制作铃声,下载音乐等等.而在Linux发行版中,缺少娱乐性一直性W ...

  6. 10-2[RF] OOB validation

    main idea: 在使用bootstrap生成gi的训练集时,会有一部分数据没有被选中,使用这一部分数据(OOB)进行validation. 1.数据没有被选中的概率 假设训练集大小为N,使用bo ...

  7. Spring、Hello Spring

    1.引入Spring jar包 2.新建一个Person 接口和Person Bean public interface PersonIService { public void helloSprin ...

  8. iOS多线程GCD(转)

    原文:http://www.cnblogs.com/pure/archive/2013/03/31/2977420.html Grand Central Dispatch (GCD)是Apple开发的 ...

  9. Android SQLite Database Tutorial

    表名: 列(字段): 联系人实体类:构造方法,setters .getters方法 File:   Contact.java package com.example.sqlitetest; publi ...

  10. T-SQL变量

    T-SQL中变量分为全局变量和局部变量,分别使用@@和@前缀. 全局变量 常用的全局变量有@@VERSION .@@IDENTITY.@@ERROR.@@ROWCOUNT 用法 select @@VE ...