【发布时间】:2014-02-26 09:25:43
【问题描述】:
我正在使用http://curl.haxx.se/libcurl/c/http-post.html 的代码尝试向我经常成功使用但使用 nodejs/javascript 的 Web 服务发出发布请求。我可以通过 javascript 获取 post 请求,但不能使用 libcurl 从 C 获取。
我的 C 代码几乎就是上面链接中的代码,
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
/* In windows, this will init the winsock stuff */
curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */
curl = curl_easy_init();
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://webservice.i.use.com/this/that");
/* Now specify the POST data */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "token=TOKEN&product=PROD&timeinterval=ONE_MINUTE&datetime=2013-10-22+19:10")
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
返回,
<Cdata>
<Data />
<Success>false</Success>
<Message>Authentication Failed</Message>
</Cdata>
同样的基本信息(URL和POST数据),使用nodejs的needle包,返回认证成功的结果。 Javascript代码是,
var needle = require('needle')
needle.post('http://webservice.i.use.com/this/that',
{token:'TOKEN' , product:'PROD' , timeinterval:'ONE_MINUTE' , datetime:'2013-10-22+19:10'},·
function(err, resp, body){
console.log(body);
});
nodejs needle 包发送的是否有一些额外的信息不是用上面的 C 代码发送的?
我也试过curl CLT
curl -data "token=...." "url ...."
但这也没有用。
最近修改了http://curl.haxx.se/libcurl/c/post-callback.html 的代码,最终得到与上述相同的身份验证错误消息。任何帮助将非常感激。
【问题讨论】:
标签: javascript c node.js post curl