c++ - 使用libcurl中发送带cookie的请求失败了
阿神
阿神 2017-04-17 11:14:03
0
1
587

需求

获取自己的CSDN资源下载列表的数据
步骤:
1.登录csdn(已成功)
2.获取登录成功后的cookie,并保存到本地(mycookie.txt)(也已成功)
3.使用之前获取的cookie访问CSDN资源下载列表(失败)

代码我已经精简了下,方便各位同仁测试(只要先使用其他浏览器登录下csdn,就可以得到cookie,然后,手动将得到的cookie复制到代码中就可以进行测试)

备:libcurl刚接触,碰到一个cookie问题,搞了下午,不仅焦头烂额而且还没成功,找不到失败的原因,如果大家方便的话,还麻烦帮忙小测试下,再次先感谢大家的帮忙~!

使用libcurl发送消息头(附带cookie信息)的时候,服务端返回“请先登录”的信息。
cookie信息是没错的,请会的朋友帮我看下,是不是发送cookie信息的写法有问题呢?


long writer(void *data, int size, int nmemb, string &content) { long sizes = size * nmemb; string temp((char*)data,sizes); content += temp; return sizes; } bool CurlInit(CURL *&curl, const char* url,string &content) { CURLcode code; string error; curl = curl_easy_init(); if (curl == NULL) { printf( "Failed to create CURL connection\n"); return false; } code = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error); if (code != CURLE_OK) { printf( "Failed to set error buffer [%d]\n", code ); return false; } curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); code = curl_easy_setopt(curl, CURLOPT_URL, url); if (code != CURLE_OK) { printf("Failed to set URL [%s]\n", error); return false; } code = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); if (code != CURLE_OK) { printf( "Failed to set redirect option [%s]\n", error ); return false; } //writer:回调函数 code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer); if (code != CURLE_OK) { printf( "Failed to set writer [%s]\n", error); return false; } //回调函数的参数:content code = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content); if (code != CURLE_OK) { printf( "Failed to set write data [%s]\n", error ); return false; } return true; } void GetDownloadList(int pageNum) { CURL *easy_handle; CURLcode res; curl_global_init(CURL_GLOBAL_ALL); easy_handle = curl_easy_init(); if(easy_handle) { string content; //get参数 char url[128] = {0}; sprintf(url,"http://download.csdn.net/my/downloads/%d",pageNum); //http请求头 struct curl_slist *headers = NULL; headers = curl_slist_append(headers,"User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0"); //模拟浏览器 headers = curl_slist_append(headers,"Host:download.csdn.net"); headers = curl_slist_append(headers,"Referer:http://www.csdn.net/"); headers = curl_slist_append(headers,"Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); headers = curl_slist_append(headers,"Accept-Language:zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"); headers = curl_slist_append(headers,"Accept-Encoding:gzip, deflate"); headers = curl_slist_append(headers,"Connection:keep-alive"); headers = curl_slist_append(headers,"Cookie:UserName=用户名;UserInfo=userinfo信息(直接从火狐浏览器抓取)"); //发送http请求头 curl_easy_setopt(easy_handle, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(easy_handle, CURLOPT_URL, url); if ( !CurlInit(easy_handle,url,content) ) { printf( "Failed to global init default [%d]\n" ); return; } //执行http请求 res = curl_easy_perform(easy_handle); if(res != CURLE_OK) fprintf(stderr,"curl_easy_perform() failed: %s\n",curl_easy_strerror(res)); double length = 0; res = curl_easy_getinfo(easy_handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD , &length); FILE * file = fopen("1.html","wb"); fseek(file,0,SEEK_SET); fwrite(content.c_str(),1,length,file); fclose(file); //释放资源 curl_slist_free_all(headers); curl_easy_cleanup(easy_handle); } curl_global_cleanup(); } int _tmain(int argc, _TCHAR* argv[]) { GetDownloadList(1); getchar(); return 0; }

返回信息

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script>
    alert("请您先登录!");
    window.location.href="http://passport.csdn.net/account/login"
</script>
</head>
<body>
</body>
</html>

Python代码
注:之前使用python代码也写过一个,可以成功获取,代码也顺便贴上,借此,可以发现和C++中的http消息请求头其实是一样的

#获取之前下载的资源列表
def GetDownloadList(page):
    #get参数
    url="http://download.csdn.net/my/downloads/"+page
    #http请求头
    #经过测试,只需发送UserName和UserInfo这2个cookie值即可
    headers={"Host": "download.csdn.net",
             "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0",
             "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
             "Accept-Language":"zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3",
             "Accept-Encoding":"gzip, deflate",
             "Referer":"http://www.csdn.net/",
             "Connection":"keep-alive",
         "Cookie":"UserName="+cookies["UserName"]+";UserInfo="+cookies["UserInfo"]

    }
    r = requests.get(url,headers = headers)
    return r.text

阿神
阿神

闭关修行中......

全員に返信(1)
Peter_Zhu

headers = curl_slist_append(headers,"Cookie:UserName=用户名;UserInfo=userinfo信息(直接从火狐浏览器抓取)");

改成

curl_easy_setopt(easy_handle, CURLOPT_COOKIE,"UserName=用户名;UserInfo=userinfo信息(直接从火狐浏览器抓取)");

试试

いいねを押す +0
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート