2017-04-15 8 views
0

私は問題があります。 ログインしたいと思っています。 ogameでURLソースコードを読んでください。 しかし、私は自分のコードを開始するたびに、私のファイルは、ログイン後に2番目のページではなく、ログインページを返します。ページにログインしてURLコンテンツを読むにはどうすればいいですか?

#include <stdio.h> 
#include <curl/curl.h> 

FILE *fptr; 

static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) 
{ 

    fptr = fopen("ogametest.html", "w"); 
    fputs((const char*)contents, (FILE*)fptr); 
    fclose (fptr); 
     //printf("%s", (char *) contents); 
    return size * nmemb; 
} 


int main(int argc, char **argv){ 
    CURL *curl; 
    CURLcode res; 
    char strlist[16000]=""; 
    char* str= &strlist[0]; 

    curl = curl_easy_init(); 
    if(curl) { 
    curl_easy_setopt(curl, CURLOPT_URL, "https://de.ogame.gameforge.com/"); 
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "usernameLogin=myLogin&passwordLogin=myPassw&serverLogin=s146-de.ogame.gameforge.com"); 
    res = curl_easy_perform(curl); 
    //curl_easy_reset(curl); 
    curl_easy_setopt(curl, CURLOPT_URL, "https://s146-de.ogame.gameforge.com/game/index.php?page=overview&relogin=1"); 

     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); 
     curl_easy_setopt(curl, CURLOPT_WRITEDATA, str); 

    res = curl_easy_perform(curl); 

    fptr = fopen("ogametest.html", "r"); 
    fgets(str, 16000, fptr); 

    if(res != CURLE_OK) 
     fprintf(stderr, "curl_easy_perform() failed: %s\n", 
       curl_easy_strerror(res)); 

    /* always cleanup */ 
    curl_easy_cleanup(curl); 
    } 
    curl_global_cleanup(); 
    return 0; 
} 

私を助けることができればうれしいです。

おはようございます

答えて

0

コードにはいくつかの問題があります。

  1. ログイン時にURLが間違っています。正しいURLは "https://de.ogame.gameforge.com/main/login"

  2. です。投稿メッセージは正しい形式ではありません。

  3. クッキーを処理する必要があります。クッキーは、ログインセッションを保存するために使用されます。クッキーがなければ、あなたはogameでリクエストするたびにメインページにリダイレクトされます。ここで

はogame

string htmlcode; 
size_t writeCallback(char* buf, size_t size, size_t nmemb, void* up){ 
for (int c = 0; c<size*nmemb; c++) 
{ 
    htmldata.push_back(buf[c]); 
} 
return size*nmemb;} 

がグローバルに定義されて

CURL* curl; 
//Local initializing of curl object 
curl = curl_easy_init(); 

//Defining parameters 
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); //print html header request/responses in console 
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, TRUE);//Follow redirects 
curl_easy_setopt(curl, CURLOPT_URL, "https://de.ogame.gameforge.com/main/login");//Url 
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie.txt");//Save cookies here 
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookie.txt");//Load cookies here 
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "kid=&login=my%40email.here&pass=mypassword&uni=s148-de.ogame.gameforge.com");//Post message 
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeCallback);//html code response function 
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30");//Setting agent. Just do this 

//Executing curl object 
curl_easy_perform(curl); 

にログインするために働くいくつかのカールコードです。このコードでは、htmlコードはhtmlcodeという文字列に格納されます。

"my%40email.here"をメールに変更する必要があります。 %40は@です。 "mypassword"はあなたのパスワードに変更する必要があります。 "s148-de.ogame.gameforge.com"はあなたがプレイする宇宙に依存します。 148は乙女座です。

あなたは何をしようとしているのか分かりませんが、プログラムを使ってhtmlリクエスト/レスポンスを傍受することをお勧めします。 Chromeでは、DevToolsの概要(ショートカットf12)に行くことができます。ネットワークの下では、ブラウザとサーバーの間のやりとりを追跡することができます。そして、それを模倣しようとすることができます。

関連する問題