c语言中的RESTful客户端
嗨,我想给远程服务器打一个RESTful电话。 url是 服务器文档说:c语言中的RESTful客户端,c,http,restful-url,C,Http,Restful Url,嗨,我想给远程服务器打一个RESTful电话。 url是 服务器文档说: Parameters 字符串用户名、字符串密码、字符串消息、字符串msisdn、字符串标记名、字符串短码、int telcoId、bool dnRequired 返回值 API将返回包含三个成员的对象 问题成功 消息 独一无二的 我用c语言制作了一个示例客户端,但它不工作。下面是我的代码 #include <stdio.h> #include <stdlib.h> #include <cur
Parameters
字符串用户名、字符串密码、字符串消息、字符串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;
}
#包括
#包括
#包括
int main(int argc,char*argv[])
{
卷曲*卷曲;
charurl[80];
卷曲编码;
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)\u容易执行(curl);
旋度\轻松\清洁(旋度);
}
返回0;
}
输出:您的
strcpy
函数会导致未定义的行为,因为您的缓冲区为80个字符,并且源字符串大于它
试试这样的
char url[]="http://bulksms.net/API.svc/sms/json/sendmessage?username=newuser&password=newpasswd&msg=test&msisdn=9999999999&tagname=Demo&shortcode=8888&telcoId=5&";
您可以使用或进行安全复制,避免未定义的行为。我解决了这个问题。实际上,在RESTful POST方法中,url和数据应该一部分一部分地添加。下面是c中用于RESTfull POST方法的成功客户端
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
int main(int argc, char *argv[])
{
CURL *curl;
CURLcode res;
char url[]= "http://bulksms.net/API.svc/sms/json/sendmessage";
char postData[] = "username=newuser&password=newpasswd&msg=test&msisdn=9999999999&tagname=Demo&shortcode=8888&telcoId=5&dnRequired=false";
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
return 0;
}
#包括
#包括
#包括
int main(int argc,char*argv[])
{
卷曲*卷曲;
卷曲编码;
字符url[]=”http://bulksms.net/API.svc/sms/json/sendmessage";
char postData[]=“username=newuser&password=newpasswd&msg=test&msisdn=9999999&tagname=Demo&shortcode=8888&telcoId=5&dnRequired=false”;
curl=curl_easy_init();
if(curl){
curl_easy_setopt(curl,CURLOPT_URL,URL);
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,postData);
res=旋度(curl)\u容易执行(curl);
旋度\轻松\清洁(旋度);
}
返回0;
}
与基于SOAP的web服务不同,RESTful web API没有“官方”标准。这是因为REST是一种体系结构样式,而SOAP是一种协议。尽管REST本身并不是一个标准,但大多数RESTful实现都使用HTTP、URI、JSON和XML等标准。我知道这是一个姗姗来迟的答案,但有一个完整而棒极了的库允许您通过C/C++引入RESTful api。适用于Linux、Freebsd和Windows(可能还有更多操作系统)
github将输出设置为写入所需的长度
HTTP错误411。请求必须分块或具有内容长度。Fyi,该字符串远远超过80个字符(更像是150个字符,包括终止符)。因此,您的
strcpy
调用了未定义的行为,而您的程序的格式不正确,或者更好-strdup
(请记住释放结果)将索引数组更改为char url[],但错误保持不变--HTTP error 411。请求必须分块或具有内容长度。。我想我必须添加一些与RESTfullremoved索引数组和使用charURL[]相关的内容,但这并没有阻止错误的发生