libcurl 模拟登录的问题求助

不要做咸鱼 2014-03-04 02:58:18
学习c++ 的libcurl过程中遇到了一些问题,在模拟登录虾米网的时候不知道为什么会出现下面的错误
< HTTP/1.1 302 Moved Temporarily
< Server: Tengine
< Date: Tue, 04 Mar 2014 06:53:39 GMT
< Content-Type: text/html; charset=utf-8
< Transfer-Encoding: chunked
< Connection: close
< Location: http://www.xiami.com
< Cache-Control: must-revalidate
< Pragma: no-cache
<
* Closing connection #0
* Issue another request to this URL: 'http://www.xiami.com'
* Violate RFC 2616/10.3.3 and switch from POST to GET
* About to connect() to www.xiami.com port 80 (#0)

下面是源码:求高人给个思路,或者给指出错误在哪谢谢了

#include <curl/curl.h>
#pragma comment(lib,"curllib.lib")
#define POSTURL "https://login.xiami.com/member/login"
#define POSTFIELDS "_xiamitoken=819938e694aa2024c49f2a0c362831e3&done=http://www.xiami.com&type=&email=112121212@qq.com&password=passwd&submit=登录"
#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_COOKIEJAR,"cookie_login.txt");//保存登陆后的cookie

curl_easy_setopt(curl,CURLOPT_COOKIEFILE,"cookie_login.txt");//提交第一步保存的cookie

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(curl, CURLOPT_CAINFO, "ca-bundle.crt"); //设置证书路径
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1L);

res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
system("pause");
}

size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) {
FILE *fptr = (FILE*)userp;
return fwrite(buffer, size, nmemb, fptr);
}
...全文
155 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
不要做咸鱼 2014-03-04
  • 打赏
  • 举报
回复
引用 3 楼 bluesky12312388 的回复:
看了下,
<script>
    _xiamitoken = '4d34e7e68d0772e9497a4891d1f54e21';
</script>
这个应该是服务端随机生成的,用来校验。 所以你要先用libcurl获取 https://login.xiami.com/member/login 这个页面,取出_xiamitoken 然后再去用这个取到的token模拟登陆。
那提示302 应该不会是token不正确吧?
bluesky12312388 2014-03-04
  • 打赏
  • 举报
回复
看了下,
<script>
    _xiamitoken = '4d34e7e68d0772e9497a4891d1f54e21';
</script>
这个应该是服务端随机生成的,用来校验。 所以你要先用libcurl获取 https://login.xiami.com/member/login 这个页面,取出_xiamitoken 然后再去用这个取到的token模拟登陆。
不要做咸鱼 2014-03-04
  • 打赏
  • 举报
回复
我擦,半天都没人么
不要做咸鱼 2014-03-04
  • 打赏
  • 举报
回复
用httpwatch 看到的post数据是这个样子的,我很纳闷这个token是从哪里获取的呢 _xiamitoken=819938e694aa2024c49f2a0c362831e3&done=http%253A%252F%252Fwww.xiami.com%252F&type=&email=5308****@qq.com&password=passwd&submit=%E7%99%BB+%E5%BD%95

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧