我想編寫一個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?
如何爲第二個請求設置標頭?,我創建了一個顯示所有cookie標頭的函數,但我不知道如何設置它們。 – patrick1337
curl_easy_setopt(myHandle,CURLOPT_COOKIEFILE,「」); curl_easy_setopt(myHandle,CURLOPT_COOKIE,「key1 = value1; key2 = value2;」); –
我爲第二個請求設置了Cookie,但它仍然無效 – patrick1337