Witam,
chciałem skorzystać z biblioteki CURL i za jej pomocą logować się na stronę i móc tam poruszać i pobierać dane do pliku.
W poniższym kodzie chcialem sie zalogowac na stronę i pobrać kod strony jednak pobiera kod strony przed logowaniem.
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <curl\curl.h>
#include <cstdlib>
using namespace std;
size_t do_pliku( char *ptr, size_t size, size_t nmemb, FILE *userdata)
{
return fwrite(ptr, size, nmemb, userdata);
}
int main(void)
{
FILE * plik;
CURL *curl;
CURLcode res;
char *url = NULL;
do{
plik = fopen("data.txt" , "w");
}while(plik == NULL);
fputs("tata",plik);
curl = curl_easy_init();
if(curl) {
// Set up a couple initial paramaters that we will not need to mofiy later.
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/4.0");
curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1 );
//////////////////////////////////////////////////////////////////
curl_easy_setopt(curl, CURLOPT_URL, "www.example.com");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "login=moj_login&haslo=moje_haslo");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookie.txt");
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie.txt");
res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
cout<<endl<<url<<endl;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, do_pliku);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, plik);
curl_easy_perform(curl);
fclose(plik);
curl_easy_cleanup(curl); // always cleanup
}
system("PAUSE");
return EXIT_SUCCESS;
}