2017-04-15 69 views
0

我遇到问题了。 我想登录例如在ogame中读取它的url源代码。 但是每次我开始我的代码时,我的文件都会在登录后返回登录页面而不是第二页。如何登录页面并从中读取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. 该网址是错误的登录。正确的网址是“https://de.ogame.gameforge.com/main/login

  2. 发布消息不是正确的格式。

  3. 您需要处理cookies。 Cookie用于保存登录会话。如果没有cookie,您每次在ogame发出任何请求时都会重定向到主页面。

下面是一些卷曲代码,工程登录ogame

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); 

其中

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

是全局定义。所以在这段代码中,html代码将被存储在字符串htmlcode中。

现在“my%40email.here”应该更改为您的电子邮件。 %40是@。 “mypassword”应该更改为您的密码。 “s148-de.ogame.gameforge.com”取决于你玩的宇宙。 148是处女座。

我不知道你在做什么,但我建议你使用一个程序来拦截html请求/响应。在Chrome中,您可以转到DevTools概述(快捷键f12)。在网络下,您可以跟踪浏览器和服务器之间的交换,然后您可以尝试模仿。

相关问题