如何在页面上登录并从中读取URL内容

How to login on a page and read url content from it?

本文关键字:读取 URL 内容 登录      更新时间:2023-10-16

我有问题。我想登录,例如在Ogame中,并从中读取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: %sn",
              curl_easy_strerror(res));
    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  curl_global_cleanup();
  return 0;
}

如果您可以帮助我,那会很好。

有美好的一天

您的代码有几个问题。

  1. 登录的URL是错误的。正确的URL是" https://de.ogame.gameforge.com/main/login"

  2. 帖子消息不是正确的格式。

  3. 您需要处理cookie。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中。

现在,应该将"我的%40email..here"更改为您的电子邮件。%40是 @。" MyPassword"应更改为您的密码。" s148-de.ogame.gameforge.com"取决于您玩的宇宙。148是处女座。

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