使用 libcURL 连续提交 (C/C++)

发布于 2024-11-15 09:20:18 字数 2257 浏览 3 评论 0原文

我是 libcURL 的新手,在执行任务时遇到问题。

我需要连续两次提交来更改文本框。

第一:网络有一个选择框(组)和一个提交按钮。

第二:第一次提交后,您会获得一个特定页面,其中包含

我的代码,但不起作用(两者都提交):

#include <stdio.h>
#include <iostream>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>

int main(void)
{
    CURL *curl;
    CURLcode res;

    struct curl_httppost *formpost = NULL;
    struct curl_httppost *lastptr = NULL;
    struct curl_slist *headerlist = NULL;
    static const char buf[] = "Expect:";

    curl_global_init(CURL_GLOBAL_ALL); 

    curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "group", CURLFORM_COPYCONTENTS, "ImageSource", CURLFORM_END);
    curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "Select group", CURLFORM_END);
    curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "root_ImageSource_I0_Name", CURLFORM_COPYCONTENTS, "weboCam", CURLFORM_END);
    curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "Save", CURLFORM_END);

    curl = curl_easy_init();
    headerlist = curl_slist_append(headerlist, buf);

    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://192.168.0.90/admin/config.shtml");
        curl_easy_setopt(curl, CURLOPT_USERPWD, "xxxx:xxxx");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
        curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);   

        res = curl_easy_perform(curl);

        long http_code = 0;

        curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);

        if (http_code == 200 && res != CURLE_ABORTED_BY_CALLBACK)
        {
            cout << "Todo ha ido perfecto." << endl;         //Succeeded
        }
        else
        {
            cout << "Ha habido un error." << endl; //Failed
        }

        curl_formfree( formpost );

        curl_easy_cleanup(curl);
    }

另一个问题:代码编译并显示一切正常,但实际上只显示默认页面代码 http ://192.168.0.90/admin/config.shtml

提前致谢

I'm new using libcURL and have a problem to do a task.

I need to do a two submits consecutives to change a textbox.

First: The web have a select box (group) and a submit button.

Second: After de first submit you obtain an specific page with

That my code, but doesn't work(both submits):

#include <stdio.h>
#include <iostream>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>

int main(void)
{
    CURL *curl;
    CURLcode res;

    struct curl_httppost *formpost = NULL;
    struct curl_httppost *lastptr = NULL;
    struct curl_slist *headerlist = NULL;
    static const char buf[] = "Expect:";

    curl_global_init(CURL_GLOBAL_ALL); 

    curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "group", CURLFORM_COPYCONTENTS, "ImageSource", CURLFORM_END);
    curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "Select group", CURLFORM_END);
    curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "root_ImageSource_I0_Name", CURLFORM_COPYCONTENTS, "weboCam", CURLFORM_END);
    curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "Save", CURLFORM_END);

    curl = curl_easy_init();
    headerlist = curl_slist_append(headerlist, buf);

    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://192.168.0.90/admin/config.shtml");
        curl_easy_setopt(curl, CURLOPT_USERPWD, "xxxx:xxxx");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
        curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);   

        res = curl_easy_perform(curl);

        long http_code = 0;

        curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);

        if (http_code == 200 && res != CURLE_ABORTED_BY_CALLBACK)
        {
            cout << "Todo ha ido perfecto." << endl;         //Succeeded
        }
        else
        {
            cout << "Ha habido un error." << endl; //Failed
        }

        curl_formfree( formpost );

        curl_easy_cleanup(curl);
    }

Another problem: The code compiles and shows that everything goes right but really only show the default page code http://192.168.0.90/admin/config.shtml

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷子口的你 2024-11-22 09:20:18

第一步完成了。

这段代码完成了第一步。

char *data1="group=ImageSource";
    char *data2="root_ImageSource_I0_Name=weboCam";

    curl = curl_easy_init();

    if(curl) 
    {
        // Put the original page
        curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.90/admin/config.shtml");
        curl_easy_setopt(curl, CURLOPT_USERPWD, "root:root");       
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data1);

选择第一个选择。
在此处输入图像描述

执行此操作后,我想修改此文本框
在此处输入图像描述

总结:我想更改页面第二级中的文本框。问题出在第二个 easy_perform 返回顶层的 libcurl 上。

First step done.

This code do the first step.

char *data1="group=ImageSource";
    char *data2="root_ImageSource_I0_Name=weboCam";

    curl = curl_easy_init();

    if(curl) 
    {
        // Put the original page
        curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.90/admin/config.shtml");
        curl_easy_setopt(curl, CURLOPT_USERPWD, "root:root");       
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data1);

Take selection of the first select.
enter image description here

After doing this I want to modify this textbox
enter image description here

Summarize: I want to change a textbox in the second level of a page. The problem is libcurl on the second easy_perform return to the top level.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文