2014-04-23 1 views
0

나는 새 트위터 나 트위터가 있으면 내 트위터에 로그온하여 경고하는 프로그램을 C++로 작성하려고했습니다. libcurl, https://www.hackthissite.org/articles/read/1078으로 웹 사이트에 로그온하는 방법을 설명하는 기사를 찾았습니다. 그것은 페이지에 로그를 다운로드 토큰 인증을 추출하여 사용자 이름과 암호를 사용하여 POST 요청에 그것을두고 보냅니다libcurl로 트위터에 로그온

#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; 
} 

: 나는 간단한 프로그램을 작성했습니다 그 기사를 사용. 유일한 문제는 로그인하는 대신 페이지에 다시 로그온한다는 것입니다. 그것은 hackthissite.org에 로그온합니다. 그래서 나는 libcurl을 사용하여 트위터에 로그온 할 수있는 방법이 있습니까 ??

답변

0

두 번째 요청에 헤더 쿠키를 설정해야합니다. 첫 번째 요청에서 반환 된 쿠키를 가져와 두 번째 요청에서 설정해야합니다. 쿠키를 보내려면, 당신이 그들을 쿠키를 활성화 한 다음 설정해야합니다

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

헤더를 구문 분석하려면 사용하는 수 CURLOPT_HEADERFUNCTION이 서명으로 함수를 통과 curl_easy_setopt에 : 블록을 size_t function(void *ptr, size_t size, size_t nmemb, void *userdata); 및 분석 (모든 헤더가 될 것입니다 HTTP 표준 (Header: value\r\n)에 따라 ptr에있는 쿠키 헤더를 찾아 두 번째 요청으로 전달하십시오.

+0

두 번째 요청의 헤더를 어떻게 설정합니까? 모든 쿠키 헤더를 표시하는 기능을 만들었지 만 쿠키 헤더를 설정하는 방법을 모르겠습니다. – patrick1337

+0

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

+0

두 번째 요청에 대한 쿠키를 설정했지만 여전히 작동하지 않습니다. – patrick1337

관련 문제