Sadow
Известный
- 1,410
- 582
Не работаетC++:size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* response) { response->append((char*)contents, size * nmemb); return size * nmemb; } int main() { CURL* curl = curl_easy_init(); if (curl) { std::string url = "http://google.com/api"; curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); std::string response; curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); CURLcode res = curl_easy_perform(curl); if (res != CURLE_OK) { std::cout << "error res one: " << curl_easy_strerror(res) << std::endl; return 1; } curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); response.clear(); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); res = curl_easy_perform(curl); if (res != CURLE_OK) { std::cout << "error res two: " << curl_easy_strerror(res) << std::endl; return 1; } std::cout << "res res one: " << response << std::endl; curl_easy_cleanup(curl); } return 0; }
Вот мой код:
говнокод на c++:
#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;
}
Последнее редактирование: