1

I want to simulate web form submission to the PHP API, but it fails, I tried a lot of methods. My code

#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;
}

I changed the parameter of post to this format and it can be sent normally like this: username = 11111 & password = 1111 & client = wap instead of using the form_add function, but I do n’t know how to do this. enter image description hereenter image description here How to correct this problem? I have no idea

jianbo
  • 23
  • 4

2 Answers2

0

You're mixing form encodings. CURLOPT_POSTFIELDS expects a URL-encoded form (e.g. username = 11111&password =1111&client=wap) whereas curl_formadd generates multipart/form-data encoded forms. Either use CURLOPT_HTTPPOST with curl_formadd, or use CURLOPT_POSTFIELDS on the url-encoded form.

In either case you don't need to add the Content-Type header since it will be added automatically.

Andrew Lambert
  • 1,869
  • 1
  • 17
  • 31
  • But I don't think it is mainly caused by this. I tried to remove the curl_slist_append (haderlist, HADES) function and it still cannot be parsed normally – jianbo Mar 13 '20 at 07:53
0
 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
jianbo
  • 23
  • 4