C++ 以libcurl作为std::string返回post值
如何提取表单中C++ 以libcurl作为std::string返回post值,c++,libcurl,C++,Libcurl,如何提取表单中id字段中的值,并将其声明为std::string 调用此函数时,它向提供的链接发出post请求,并将结果输出到客户端的命令行 int push_(void) { CURL* curl; CURLcode res; /* In windows, this will init the winsock stuff */ curl_global_init(CURL_GLOBAL_ALL); /* get a curl handle */
id
字段中的值,并将其声明为std::string
调用此函数时,它向提供的链接发出post请求,并将结果输出到客户端的命令行
int push_(void)
{
CURL* curl;
CURLcode res;
/* In windows, this will init the winsock stuff */
curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */
curl = curl_easy_init();
if (curl) {
/* First set the URL that is about to receive our POST. This URL can
just as well be a https:// URL if that is what should receive the
data. */
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost/api/0000/push");
/* Now specify the POST data */
// the form on the server side has a single field
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "id=");
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
作为输入提供的id
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
服务器响应
fffffffffffffffffffffffffffffffffffffffffffffffffffffffff<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>Redirecting...</title>
<h1>Redirecting...</h1>
<p>You should be redirected automatically to target URL: <a href="/login">/login</a>.
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
重定向。。。
重定向。。。
您应该自动重定向到目标URL:。
我已经提取了id值,但我不知道如何将id从html标记中分离出来您必须添加一个回调函数
CurlWrite_CallbackFunc_StdString
,以从post请求中获取数据
#include <iostream>
#include <string>
#include <curl/curl.h>
size_t CurlWrite_CallbackFunc_StdString(void* contents, size_t size, size_t nmemb, std::string* s)
{
size_t newLength = size * nmemb;
try
{
s->append((char*)contents, newLength);
}
catch (std::bad_alloc& e)
{
//handle memory problem
return 0;
}
return newLength;
}
std::string h3ll0() {
CURL* curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
std::string s;
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "localhost/api/0000/hello");
// Add post data to get the command line from the server post request
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "id=");
// A calback function that copies the data from the post request output
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWrite_CallbackFunc_StdString);
// Write the copied data to the variable s to return it
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
/* always cleanup */
curl_easy_cleanup(curl);
}
//std::cout << s << std::endl;
//std::cout << "Program finished!" << std::endl;
return s;
}
int main() {
std::string result = h3ll0();
std::cout << result << std::endl;
}
#包括
#包括
#包括
size_t CurlWrite_CallbackFunc StdString(void*内容、size_t大小、size_t nmemb、std::string*s)
{
size\u t newLength=大小*nmemb;
尝试
{
s->append((char*)内容,newLength);
}
捕获(标准::错误分配和e)
{
//处理内存问题
返回0;
}
返回newLength;
}
std::字符串h3ll0(){
卷曲*卷曲;
卷曲编码;
curl\u global\u init(curl\u global\u默认值);
curl=curl_easy_init();
std::字符串s;
if(curl)
{
curl_easy_setopt(curl,CURLOPT_URL,“localhost/api/0000/hello”);
//添加post数据以从服务器post请求获取命令行
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,“id=”);
//从post请求输出复制数据的calback函数
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,CurlWrite_CallbackFunc_StdString);
//将复制的数据写入变量s以返回它
curl_easy_setopt(curl、CURLOPT_WRITEDATA和s);
/*执行请求,res将获得返回代码*/
res=旋度(curl)\u容易执行(curl);
/*检查错误*/
如果(res!=卷曲(OK)
{
fprintf(stderr,“curl\u easy\u perform()失败:%s\n”,
卷曲(容易的);
}
/*总是清理*/
旋度\轻松\清洁(旋度);
}
//标准::cout