【发布时间】:2010-04-21 12:40:19
【问题描述】:
我需要使用 libcurl 将文件发送到网络服务器。我在 curl 网站上看到了其中一个示例,并正在尝试实现它。这是postit2.c 示例。有人可以告诉我如何扩展它以便能够发送用户名和密码
【问题讨论】:
我需要使用 libcurl 将文件发送到网络服务器。我在 curl 网站上看到了其中一个示例,并正在尝试实现它。这是postit2.c 示例。有人可以告诉我如何扩展它以便能够发送用户名和密码
【问题讨论】:
使用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" />
【讨论】:
?user=username&pass=mypass
例如,看 'res = curl_easy_perform(curl);'部分 postit2.c ...您可以添加 'printf("CurlCode: %d", res);'。如果result为0,则表示提交成功。
【讨论】: