C++ 使用C++;LibCurl身份验证以同时请求多个URL
我正在做一个小项目来登录一个网站,这取决于传递的参数数量,我希望它同时请求每个页面从中提取数据。当我只使用一个时,它工作得很好,当我使用线程并在循环中的每个触发器后连接它们时,它们不会执行它们的卷曲动作,我假设是因为句柄试图同时被使用。我想知道如何合并多句柄并将其附加到我的句柄上,以使用已验证的句柄执行多个请求。 我假设multi-handle就是解决方案,但我似乎不知道如何合并它并使用初始句柄发送请求 谢谢大家!C++ 使用C++;LibCurl身份验证以同时请求多个URL,c++,libcurl,C++,Libcurl,我正在做一个小项目来登录一个网站,这取决于传递的参数数量,我希望它同时请求每个页面从中提取数据。当我只使用一个时,它工作得很好,当我使用线程并在循环中的每个触发器后连接它们时,它们不会执行它们的卷曲动作,我假设是因为句柄试图同时被使用。我想知道如何合并多句柄并将其附加到我的句柄上,以使用已验证的句柄执行多个请求。 我假设multi-handle就是解决方案,但我似乎不知道如何合并它并使用初始句柄发送请求 谢谢大家! curl_easy_setopt(myHandle, CURLOPT_USERA
curl_easy_setopt(myHandle, CURLOPT_USERAGENT, "Mozilla/5.0");
curl_easy_setopt(myHandle, CURLOPT_AUTOREFERER, 1);
curl_easy_setopt(myHandle, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(myHandle, CURLOPT_COOKIEFILE, "");
// Visit the login page once to obtain a PHPSESSID cookie
curl_easy_setopt(myHandle, CURLOPT_URL, "https://example.com/login.php");
curl_easy_setopt(myHandle, CURLOPT_WRITEFUNCTION, WriteCallback2);
curl_easy_perform(myHandle);
CURLcode res;
// Now, can actually login. First we forge the HTTP referer field, or HTS will deny the login
curl_easy_setopt(myHandle, CURLOPT_REFERER, "https://example.com/login.php");
// Next we tell LibCurl what HTTP POST data to submit
const char* data = "USER=user&PASSWORD=password";
curl_easy_setopt(myHandle, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(myHandle, CURLOPT_WRITEFUNCTION, WriteCallback2);
curl_easy_perform(myHandle);
if(argc>3){
int i=2;
int max_shids=argc-2;
向量线程;
对于(i=2;argv[i]!=nullptr;i++){
字符串shid=argv[i];
库特
if (argc > 3) {
int i = 2;
int max_shids = argc - 2;
std::vector<std::thread> threads;
for (i = 2; argv[i] != nullptr; i++) {
string shid = argv[i];
cout << shid << endl;
threads.push_back(std::thread(myFunc, myHandle, shid, closetime));
}
for (std::thread& t : threads)
{
t.join();
}
}
void myFunc(CURL* myHandle, string shid, int wait_time) {
int still_alive = 1;
Sleep(wait_time);
cout << shid << endl;
auto start = std::chrono::system_clock::now();
std::string readBuffer;
CURLcode res;
CURLM* cm;
string shidurl = "https://example.com/items?=" + shid;
curl_easy_setopt(myHandle, CURLOPT_URL, shidurl.c_str());
readBuffer.clear();
curl_easy_setopt(myHandle, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(myHandle, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_perform(myHandle);
//std::string(readBuffer.begin(), readBuffer.end());
string s = std::string(readBuffer.begin(), readBuffer.end());
ofstream myfile;
myfile.open(shid+".html");
myfile << readBuffer;
myfile.close();
ifstream data_store;
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
std::cout << "finished computation at " << std::ctime(&end_time)
<< "elapsed time: " << elapsed_seconds.count() << "s\n";
cv.notify_one();
return;
}