| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3916 人关注过本帖
标题:libcurl 做的登录 但是登录不进去 求解
只看楼主 加入收藏
jsjseo
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-6-14
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
libcurl 做的登录 但是登录不进去 求解
贴下源码  有点乱  email和password用**号代替

#include "stdafx.h"
#include "curl\curl.h"
#include <string>
#include <assert.h>

using namespace std;

#pragma comment(lib,"libcurl_imp.lib")

#define  SKIP_PEER_VERIFICATION 1  
//#define  SKIP_HOSTNAME_VERFICATION 1  

/*
ptr是指向存储数据的指针,
size是每个块的大小,
nmemb是指块的数目,
stream是用户参数。
所以根据以上这些参数的信息可以知道,ptr中的数据的总长度是size*nmemb
*/
size_t call_wirte_func(const char *ptr, size_t size, size_t nmemb, std::string *stream)
{
    assert(stream != NULL);
    size_t len = size * nmemb;
    stream->append(ptr, len);
    return len;
}
// 返回http header回调函数   
size_t header_callback(const char  *ptr, size_t size, size_t nmemb, std::string *stream)
{
    assert(stream != NULL);
    size_t len = size * nmemb;
    stream->append(ptr, len);
    return len;
}
int _tmain(int argc, _TCHAR* argv[])
{
    CURL *curl;
    CURLcode code;
    std::string szbuffer;
    string recvbuffer;
    std::string szheader_buffer;
    char errorBuffer[CURL_ERROR_SIZE];
    std::string url = "https://www.
    //std::string url = "https://vip.  
    std::string useragent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1";

    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();
    if (curl) {
        // 远程URL,支持 http, https, ftp  
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_USERAGENT, useragent.c_str());
        // 官方下载的DLL并不支持GZIP,Accept-Encoding:deflate, gzip  
        curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip, deflate");
        //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);//调试信息打开  
        //https 访问专用:start  
        #ifdef SKIP_PEER_VERIFICATION  
        //跳过服务器SSL验证,不使用CA证书  
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
        //如果不跳过SSL验证,则可指定一个CA证书目录  
        //curl_easy_setopt(curl, CURLOPT_CAPATH, "this is ca ceat");  
        #endif  

        #ifdef SKIP_HOSTNAME_VERFICATION  
        //验证服务器端发送的证书,默认是 2(高),1(中),0(禁用)  
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
        #endif  
        //https 访问专用:end  


        //发送cookie值给服务器  
        //curl_easy_setopt(curl, CURLOPT_COOKIE, "name1=var1; name2=var2;");   
        /* 与服务器通信交互cookie,默认在内存中,可以是不存在磁盘中的文件或留空 */
        curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "./cookie.txt");
        /* 与多个CURL或浏览器交互cookie,会在释放内存后写入磁盘文件 */
        curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "./cookie.txt");

        /* POST 数据 */
        // curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl");  
        //设置重定向的最大次数  
        curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 5);
        //设置301、302跳转跟随location  
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
        //抓取内容后,回调函数  
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, call_wirte_func);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &szbuffer);
        //抓取头信息,回调函数  
        curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback);
        curl_easy_setopt(curl, CURLOPT_HEADERDATA, &szheader_buffer);
        code = curl_easy_perform(curl);
        if (CURLE_OK == code) {

            const char* appActionTokenStart = "<input type=\"hidden\" name=\"appActionToken\" value=\"";
            const char*  appActionStart = "<input type=\"hidden\" name=\"appAction\" value=\"";
            const char*  openid_pape_max_auth_ageStart = "<input type=\"hidden\" name=\"openid.pape.max_auth_age\" value=\"";
            const char*  openid_nsStart = "<input type=\"hidden\" name=\"openid.ns\" value=\"";
            const char*  openid_ns_papeStart = "<input type=\"hidden\" name=\"openid.ns.pape\" value=\"";
            const char*  pageIdStart = "<input type=\"hidden\" name=\"pageId\" value=\"";
            const char*  openid_identityStart = "<input type=\"hidden\" name=\"openid.identity\" value=\"";
            const char*  openid_claimed_idStart = "<input type=\"hidden\" name=\"openid.claimed_id\" value=\"";
            const char*  openid_modeStart = "<input type=\"hidden\" name=\"openid.mode\" value=\"";
            const char*  openid_assoc_handleStart = "<input type=\"hidden\" name=\"openid.assoc_handle\" value=\"";
            const char*  openid_return_toStart = "<input type=\"hidden\" name=\"openid.return_to\" value=\"";

            int IappActionToken = szbuffer.find(appActionTokenStart);
            string appActionToken = szbuffer.substr(IappActionToken + strlen(appActionTokenStart), 50);
            appActionToken = appActionToken.substr(0, appActionToken.find_first_of('"'));
            string appAction = szbuffer.substr(szbuffer.find(appActionStart) + strlen(appActionStart), 6);
            string openid_pape_max_auth_age = szbuffer.substr(szbuffer.find(openid_pape_max_auth_ageStart) + strlen(openid_pape_max_auth_ageStart), 8);
            string openid_ns = szbuffer.substr(szbuffer.find(openid_nsStart) + strlen(openid_nsStart), 48);
            string openid_ns_pape = szbuffer.substr(szbuffer.find(openid_ns_papeStart) + strlen(openid_ns_papeStart), 64);
            string pageId = szbuffer.substr(szbuffer.find(pageIdStart) + strlen(pageIdStart), 12);
            string openid_identity = szbuffer.substr(szbuffer.find(openid_identityStart) + strlen(openid_identityStart), 72);
            string openid_claimed_id = szbuffer.substr(szbuffer.find(openid_claimed_idStart) + strlen(openid_claimed_idStart), 72);
            string openid_mode = szbuffer.substr(szbuffer.find(openid_modeStart) + strlen(openid_modeStart), 24);
            string openid_assoc_handle = szbuffer.substr(szbuffer.find(openid_assoc_handleStart) + strlen(openid_assoc_handleStart), 12);
            string openid_return_to = szbuffer.substr(szbuffer.find(openid_return_toStart) + strlen(openid_return_toStart), 200);
            openid_return_to = openid_return_to.substr(0, openid_return_to.find_first_of('"'));
            string email = "*****@
            string create = "0";
            string password = "******";
            string metadata1 = "";

            string postData = "appActionToken=" + appActionToken + "&" + "appAction=" + appAction +
                "&" + "openid.pape.max_auth_age=" + openid_pape_max_auth_age + "&" + "openid.ns=" + openid_ns +
                "&" + "openid.ns.pape=" + openid_ns_pape +
                "&" + "pageId=" + pageId + "&" + "openid.identity=" + openid_identity +
                "&" + "openid.claimed_id=" + openid_claimed_id + "&" + "openid.mode=" + openid_mode +
                "&" + "openid.assoc_handle=" + openid_assoc_handle + "&" + "openid.return_to=" + openid_return_to +
                "&" + "email=" + email + "&" + "create=" + create + "&" + "password=" + password +
                "&" + "metadata1=" + metadata1;


            curl_easy_setopt(curl, CURLOPT_URL, "https://www.);
            curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
            curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1);
            curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
            curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie.txt");
            curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookie.txt");

            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, call_wirte_func);
            curl_easy_setopt(curl, CURLOPT_WRITEDATA, &recvbuffer);
            curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData);
            curl_easy_perform(curl);

        }
        else {
            fprintf(stderr, "Failed to get '%s' [%s]\n", url, errorBuffer);
            // exit(EXIT_FAILURE);  
        }

        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();

    getchar();
    return 0;
}
搜索更多相关主题的帖子: password comment include stream email 
2014-06-14 14:37
砖家的谎言
Rank: 12Rank: 12Rank: 12
等 级:禁止访问
威 望:30
帖 子:693
专家分:3898
注 册:2013-12-6
收藏
得分:10 
用户名和密码匹配登陆问题

我不是砖家,要努力成为砖家。
2014-06-15 14:22
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6814
专家分:42393
注 册:2010-12-16
收藏
得分:10 
调试下,看看错误码
curl 有错误码的,你这样贴代码没有谁那么容易帮到你的

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2014-06-15 20:48
快速回复:libcurl 做的登录 但是登录不进去 求解
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013123 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved