0

我正在尝试使用 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);
}
4

2 回答 2

0

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

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "HelloWorld"); 
于 2015-04-23T10:54:55.727 回答
0

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

于 2012-08-24T17:01:51.443 回答