【问题标题】:Sending form in libcurl在 libcurl 中发送表单
【发布时间】:2010-04-21 12:40:19
【问题描述】:

我需要使用 libcurl 将文件发送到网络服务器。我在 curl 网站上看到了其中一个示例,并正在尝试实现它。这是postit2.c 示例。有人可以告诉我如何扩展它以便能够发送用户名和密码

【问题讨论】:

    标签: c forms libcurl http-post


    【解决方案1】:

    使用curl_formadd 向 POST 数据添加更多字段。

    如果您想向该示例添加代码,您可以在正在设置表单的部分中执行,就在注释上方:/* Fill in the submit field too, even if this is rarely needed */

    你要添加的代码是这样的:

    curl_formadd(&formpost,
                 &lastptr,
                 CURLFORM_COPYNAME, "user", //the name of the data to send
                 CURLFORM_COPYCONTENTS, "username", //the users username
                 CURLFORM_END);
    
    curl_formadd(&formpost,
                 &lastptr,
                 CURLFORM_COPYNAME, "pass", //the name of the data to send
                 CURLFORM_COPYCONTENTS, "mypass", //the users password
                 CURLFORM_END);
    

    提交相同数据的 HTML 表单(假设用户输入了正确的密码)如下所示:

    Username: <input type="text" name="user" /> <br />
    Password: <input type="password" name="pass" />
    

    【讨论】:

    • 你能告诉我发送表单在 html 中的样子吗?我试图使用wireshark对此进行测试,但我看不到html代码本身。
    • @sfactor 完成。 HTML 代码不会在 POST 请求中发送,在表单中输入的值将转换为键/值字符串,例如 ?user=username&amp;pass=mypass
    • 我怎么知道在这种情况下提交是否成功?
    【解决方案2】:

    例如,看 'res = curl_easy_perform(curl);'部分 postit2.c ...您可以添加 'printf("CurlCode: %d", res);'。如果result为0,则表示提交成功。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-28
      • 2021-11-10
      • 1970-01-01
      • 2016-01-15
      相关资源
      最近更新 更多