【发布时间】:2014-12-18 05:14:02
【问题描述】:
您好,我想对远程服务器进行 RESTful 调用。 网址是http://bulksms.net/API.svc/sms/json/sendmessage?username=newuser&password=newpasswd&msg=test&msisdn=9999999999&tagname=Demo&shortcode=8888&telcoId=5&dnRequired=false 服务器文档说:
Parameters
字符串用户名、字符串密码、字符串 msg、字符串 msisdn、字符串标记名、字符串短代码、int telcoId、bool dnRequired
返回值 API 将返回包含三个成员的对象 成功 信息 唯一标识
我在 c 中制作了一个示例客户端,但它无法正常工作。下面是我的代码。
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
int main(int argc, char *argv[])
{
CURL *curl;
char url[80];
CURLcode res;
strcpy(url,"http://bulksms.net/API.svc/sms/json/sendmessage?username=newuser&password=newpasswd&msg=test&msisdn=9999999999&tagname=Demo&shortcode=8888&telcoId=5&");
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POST, url);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
return 0;
}
输出:
【问题讨论】:
-
msd 输出以写入
要求长度
HTTP 错误 411。请求必须分块或具有内容长度。
-
仅供参考,该字符串远超过 80 个字符(它更像是 150 个字符,包括终止符)。因此,您的
strcpy会调用未定义的行为,并且您的程序格式错误 -
甚至更好 -
strdup(但请记住释放结果) -
将索引数组更改为 char url[] 但错误仍然相同----
HTTP 错误 411。请求必须分块或具有内容长度。
。我想我必须添加一些与 RESTfull 相关的内容
标签: c http restful-url