2014-04-23 38 views
0

我想编写一个C++程序,该程序将登录到我的Twitter并警告我是否有任何新消息或推文。我发现了一篇文章,解释了如何使用libcurl登录到网站,https://www.hackthissite.org/articles/read/1078。使用该文章中,我已经写了一个简单的程序:用libcurl登录到twitter

#include <ios> 
#include <sstream> 
#include <iostream> 
#include <stdio.h> 
#include <stdlib.h> 
#include <curl/curl.h> 
#include <string.h> 
#include <string> 
#include <fstream> 

using namespace std; 

size_t write_to_string(void *ptr, size_t size, size_t count, void *stream) { 
((string*)stream)->append((char*)ptr, 0, size*count); 
return size*count; 
} 



/*this function logs on to the website */ 
int login(char username[24],char password[24]) 
{ 

curl_global_init(CURL_GLOBAL_ALL); 
CURL * myHandle = curl_easy_init (); 
// Set some initial paramaters 

curl_easy_setopt(myHandle, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16"); 
curl_easy_setopt(myHandle, CURLOPT_AUTOREFERER, 1); 
curl_easy_setopt(myHandle, CURLOPT_FOLLOWLOCATION, 1); 
curl_easy_setopt(myHandle, CURLOPT_COOKIEFILE, ""); 


//turn the output in to a string using a function 
string response; 
curl_easy_setopt(myHandle, CURLOPT_WRITEFUNCTION, write_to_string); 
curl_easy_setopt(myHandle, CURLOPT_WRITEDATA, &response); 


// Visit the login page once to obtain a PHPSESSID cookie 
curl_easy_setopt(myHandle, CURLOPT_URL, "https://mobile.twitter.com/session/new"); 
curl_easy_perform(myHandle); 


    //find the authenticity token in the html 
    size_t pos1 = response.find("type=\"hidden\" value=\""); 
    size_t pos2 = response.find("/></span>"); 

    string auth_token = response.substr(pos1,pos2-pos1); 

    auth_token.erase (1,21); 
    auth_token.erase (20,2); 


    //create a post request 

stringstream postreq; 
postreq << "authenticity_token=" << auth_token; 
postreq << "&username=" << username; 
postreq << "&password=" << password; 


cout << postreq.str() << endl; 

//convert the string into an array 
char *post_request_gen = (char*)malloc(248); 
strcpy(post_request_gen, postreq.str().c_str());  



//post mode 
curl_easy_setopt(myHandle, CURLOPT_POST, 1); 

//turn the output in to a string using a function 
string response2; 
    curl_easy_setopt(myHandle, CURLOPT_WRITEFUNCTION, write_to_string); 
    curl_easy_setopt(myHandle, CURLOPT_WRITEDATA, &response2); 


curl_easy_setopt(myHandle, CURLOPT_REFERER, "https://mobile.twitter.com/session/new"); 

curl_easy_setopt(myHandle, CURLOPT_POSTFIELDS, post_request_gen); 
curl_easy_perform(myHandle); 
curl_easy_cleanup(myHandle); 

//output the HTML 
cout << response2; 

//find out if the account works by searching a keyword only present after loging in 
string word1 = "logout"; 
size_t found = response2.find(word1); 
if (found!=std::string::npos) 
cout << "Account works"; 

//This is where I'll create a function that's going to check for tweets or messages 



else 
cout << "Wrong username or password" << "\n"; 
return 0; 
} 


int main() 
{ 


login("username", "password"); 



return 0; 
} 

它下载日志页面上,提取的真实性令牌和与用户名和密码,并将其放在对POST请求,并将其发送。 唯一的问题是,不是登录而是再次返回登录页面。它登录到hackthissite.org,所以我有任何方式使用libcurl登录到Twitter?

回答

0

您需要为第二个请求设置头Cookie。您必须获得从第一个请求返回的cookie,并在第二个请求中设置它们。要发送的cookie,您必须启用cookie,然后将它们设置:

curl_easy_setopt(myHandle, CURLOPT_COOKIEFILE, ""); 
curl_easy_setopt(myHandle, CURLOPT_COOKIE, "key1=value1; key2=value2;" 

为了解析头,你可以使用CURLOPT_HEADERFUNCTIONcurl_easy_setopt传递函数与此签名:size_t function(void *ptr, size_t size, size_t nmemb, void *userdata);和解析块(所有头会在ptr中,遵循HTTP标准,即Header: value\r\n),找到Cookie标头并传递给第二个请求。

+0

如何为第二个请求设置标头?,我创建了一个显示所有cookie标头的函数,但我不知道如何设置它们。 – patrick1337

+0

curl_easy_setopt(myHandle,CURLOPT_COOKIEFILE,“”); curl_easy_setopt(myHandle,CURLOPT_COOKIE,“key1 = value1; key2 = value2;”); –

+0

我为第二个请求设置了Cookie,但它仍然无效 – patrick1337