#include <iostream>
#include <regex>
#include <curl/curl.h>
using namespace std;
size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* response);
int main()
{
CURL* curl = curl_easy_init();
string resp;
smatch m;
string token;
curl_httppost* formpost = NULL;
curl_httppost* lastptr = NULL;
curl_slist* headers = curl_slist_append(NULL, "Content-Type: multipart/form-data");
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "https://www.blast.hk/members/451976/");
curl_easy_setopt(curl, CURLOPT_COOKIE, "xf_user=VALUE");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resp);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
cout << "Error : " << curl_easy_strerror(res) << endl;
return 1;
}
if (regex_search(resp, m, regex("csrf: '(\\S*)'")))
if (m.size() > 0)
token = m.str(1);
resp.clear();
curl_easy_setopt(curl, CURLOPT_URL, "https://www.blast.hk/members/451976/post");
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "xfToken",
CURLFORM_COPYCONTENTS, token,
CURLFORM_END);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "_xfToken",
CURLFORM_COPYCONTENTS, token,
CURLFORM_END);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "_xfResponseType",
CURLFORM_COPYCONTENTS, "json",
CURLFORM_END);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "message_html",
CURLFORM_COPYCONTENTS, "<p>asd</p>",
CURLFORM_END);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resp);
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
cout << "Error: " << curl_easy_strerror(res) << endl;
return 1;
}
cout << resp << endl;
curl_formfree(formpost);
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
}
size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* response)
{
response->append((char*)contents, size * nmemb);
return size * nmemb;
}