C/C libcurl如何POST表单提交,只是普通的web表单提交数据
C / C libcurl how to POST form submission, just ordinary web form submission data
我想模拟web表单提交到PHPAPI,但是失败了,我试了很多方法。
我的代码
#define HADES "Content-Type:application/x-www-form-urlencoded;charset=utf-8"
int SendPacket(char *packet, const char *server)
{
curl_global_init(CURL_GLOBAL_ALL);
CURL *curl = curl_easy_init();
if (!curl) {
return -1;
}
CURLcode ret;
struct curl_slist *haderlist = NULL;
struct curl_httppost *post = NULL;
struct curl_httppost *last = NULL;
haderlist = curl_slist_append(haderlist, HADES);
curl_formadd(&post, &last, CURLFORM_COPYNAME, "username",
CURLFORM_COPYCONTENTS,
"11111111111", CURLFORM_END);
curl_formadd(&post, &last, CURLFORM_COPYNAME, "password",
CURLFORM_COPYCONTENTS,
"111111", CURLFORM_END);
curl_formadd(&post, &last, CURLFORM_COPYNAME, "client", CURLFORM_FILE,
"wap", CURLFORM_END);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, haderlist);
curl_easy_setopt(curl, CURLOPT_URL, server);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
//SendData
ret = curl_easy_perform(curl);
if (ret != CURLE_OK ) {
curl_easy_strerror(ret);
return -1;
}
return 0;
}
我把post的参数改成这个格式可以正常发送了这样:username=11111&password=1111&client=wap 而不是使用form_add函数,但是我不知道该怎么做。
如何纠正这个问题?我不知道
您正在混合使用表单编码。 CURLOPT_POSTFIELDS
期望 URL-encoded 形式(例如 username = 11111&password =1111&client=wap
),而 curl_formadd
生成 multipart/form-data
编码形式。将 CURLOPT_HTTPPOST 与 curl_formadd
一起使用,或者在 url-encoded 表格上使用 CURLOPT_POSTFIELDS
。
无论哪种情况,您都不需要添加 Content-Type
header,因为它会自动添加。
tried to use mime to successfully solve the code as follows
int SendPacket(char *packet, const char *server)
{
curl_global_init(CURL_GLOBAL_ALL);
CURL *curl = curl_easy_init();
if (!curl) {
return -1;
}
CURLcode ret;
curl_mime *mime;
curl_mimepart *part1;
curl_mimepart *part2;
curl_mimepart *part3;
mime = curl_mime_init(curl);
part1 = curl_mime_addpart(mime);
part2 = curl_mime_addpart(mime);
part3 = curl_mime_addpart(mime);
//Add part to the mime handle, each of which is a pair of key(name)value(data)
curl_mime_data(part1, "111111", CURL_ZERO_TERMINATED);
curl_mime_name(part1,"username");
curl_mime_data(part2, "111111", CURL_ZERO_TERMINATED);
curl_mime_name(part2, "password");
curl_mime_data(part3, "wap", CURL_ZERO_TERMINATED);
curl_mime_name(part3, "client");
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
curl_easy_setopt(curl, CURLOPT_URL, server);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
ret = curl_easy_perform(curl);
if (ret != CURLE_OK ) {
curl_easy_strerror(ret);
return -1;
}
return 0;
}
Above is mock to simulate web form submission
我想模拟web表单提交到PHPAPI,但是失败了,我试了很多方法。 我的代码
#define HADES "Content-Type:application/x-www-form-urlencoded;charset=utf-8"
int SendPacket(char *packet, const char *server)
{
curl_global_init(CURL_GLOBAL_ALL);
CURL *curl = curl_easy_init();
if (!curl) {
return -1;
}
CURLcode ret;
struct curl_slist *haderlist = NULL;
struct curl_httppost *post = NULL;
struct curl_httppost *last = NULL;
haderlist = curl_slist_append(haderlist, HADES);
curl_formadd(&post, &last, CURLFORM_COPYNAME, "username",
CURLFORM_COPYCONTENTS,
"11111111111", CURLFORM_END);
curl_formadd(&post, &last, CURLFORM_COPYNAME, "password",
CURLFORM_COPYCONTENTS,
"111111", CURLFORM_END);
curl_formadd(&post, &last, CURLFORM_COPYNAME, "client", CURLFORM_FILE,
"wap", CURLFORM_END);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, haderlist);
curl_easy_setopt(curl, CURLOPT_URL, server);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
//SendData
ret = curl_easy_perform(curl);
if (ret != CURLE_OK ) {
curl_easy_strerror(ret);
return -1;
}
return 0;
}
我把post的参数改成这个格式可以正常发送了这样:username=11111&password=1111&client=wap 而不是使用form_add函数,但是我不知道该怎么做。
您正在混合使用表单编码。 CURLOPT_POSTFIELDS
期望 URL-encoded 形式(例如 username = 11111&password =1111&client=wap
),而 curl_formadd
生成 multipart/form-data
编码形式。将 CURLOPT_HTTPPOST 与 curl_formadd
一起使用,或者在 url-encoded 表格上使用 CURLOPT_POSTFIELDS
。
无论哪种情况,您都不需要添加 Content-Type
header,因为它会自动添加。
tried to use mime to successfully solve the code as follows
int SendPacket(char *packet, const char *server)
{
curl_global_init(CURL_GLOBAL_ALL);
CURL *curl = curl_easy_init();
if (!curl) {
return -1;
}
CURLcode ret;
curl_mime *mime;
curl_mimepart *part1;
curl_mimepart *part2;
curl_mimepart *part3;
mime = curl_mime_init(curl);
part1 = curl_mime_addpart(mime);
part2 = curl_mime_addpart(mime);
part3 = curl_mime_addpart(mime);
//Add part to the mime handle, each of which is a pair of key(name)value(data)
curl_mime_data(part1, "111111", CURL_ZERO_TERMINATED);
curl_mime_name(part1,"username");
curl_mime_data(part2, "111111", CURL_ZERO_TERMINATED);
curl_mime_name(part2, "password");
curl_mime_data(part3, "wap", CURL_ZERO_TERMINATED);
curl_mime_name(part3, "client");
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
curl_easy_setopt(curl, CURLOPT_URL, server);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
ret = curl_easy_perform(curl);
if (ret != CURLE_OK ) {
curl_easy_strerror(ret);
return -1;
}
return 0;
}
Above is mock to simulate web form submission