【问题标题】:Using libcurl to mimic curl使用 libcurl 模拟 curl
【发布时间】:2012-08-24 15:10:09
【问题描述】:

我正在尝试使用 libcurl 在 C 程序中模仿以下行 -

curl -d "HelloWorld" http://testserver/messaging/topic

以上工作 - 我得到了我的信息。

基本上,我要做的就是向 URL 发布一个简单的字符串。我有这个(取自一个例子),但它只是给了我 -

输入

JBossWeb/2.0.1.GA - 错误报告

HTTP 状态 405 -

类型状态报告

消息

描述 指定的HT 请求的资源 () 不允许使用 TP 方法。

JBossWeb/2.0.1.GA

输出

另外,我如何传递文本? - 通过将“?HelloWorld”附加到 URL?

#include "curl/curl.h"
void main(int argc,char **argv)
{
fprintf(stderr, "IN\n");
    CURL *curl;
    CURLcode res;

    curl = curl_easy_init();
    if (curl) {
            curl_easy_setopt(curl, CURLOPT_URL, "http://testserver/messaging/topic/");
            res = curl_easy_perform(curl);
            if (res != CURLE_OK) {
                    fprintf(stderr, "failed %s\n", curl_easy_strerror(res));
            }
            curl_easy_cleanup(curl);
    }
fprintf(stderr, "OUT\n");
    exit(0);
}

【问题讨论】:

    标签: curl libcurl


    【解决方案1】:

    在命令行中添加“--libcurl example.c”。然后将该代码构建到应用程序并运行它...

    【讨论】:

      【解决方案2】:

      -d 表示 POST 请求,因此您需要使用 curl_easy_setopt 进行设置

      curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "HelloWorld"); 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-23
        • 2010-12-02
        • 1970-01-01
        • 2011-03-18
        • 1970-01-01
        • 2023-03-31
        • 2011-02-22
        • 1970-01-01
        相关资源
        最近更新 更多