#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
#define SA struct sockaddr
#define MAXLINE 4096
#define MAXSUB 2000
#define MAXPARAM 2048
#define LISTENQ 1024
//线上线下接口宏开关
#define ONLINE
extern int h_errno;
int sockfd;
char *hostname = "";//请求地址请参考云通讯自助通平台查看或者询问您的商务负责人获取
char *send_sms_uri = "/msg/send/json";
char *query_balance_uri = "/msg/balance/json";
/**
* * 发http post请求
* */
ssize_t http_post(char *page, char *poststr)
{
char sendline[MAXLINE + 1], recvline[MAXLINE + 1];
ssize_t n;
snprintf(sendline, MAXSUB,
"POST %s HTTP/1.1 "
"Host: vsms.253.com "
"Content-type: application/json "
"Content-length: %zu "
"%s", page, strlen(poststr), poststr);
//, page, poststr);
write(sockfd, sendline, strlen(sendline));
printf(" %s", sendline);
printf(" -------------------------- ");
while ((n = read(sockfd, recvline, MAXLINE)) > 0) {
recvline[n] = '';
printf("%s ", recvline);
}
return n;
}
/**
* * 查账户余额
* */
ssize_t get_balance(char *account, char *password)
{
char params[MAXPARAM + 1];
char *cp = params;
sprintf(cp,"account=%s&password=%s", account, password);
return http_post(query_balance_uri, cp);
}
/**
* * 发送短信
* */
ssize_t send_sms(char *account, char *password, char *phone, char *msg)
{
char params[MAXPARAM + 1];
char *cp = params;
sprintf(cp,"{"account":"%s","password":"%s","phone":"%s","msg":"%s","report":"false"}", account, password, phone, msg);
return http_post(send_sms_uri, cp);
}
int main(void)
{
struct sockaddr_in servaddr;
char str[50];
//建立socket连接
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_addr.s_addr = inet_addr(hostname);
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(80);
inet_pton(AF_INET, str, &servaddr.sin_addr);
connect(sockfd, (SA *) & servaddr, sizeof(servaddr));
char *account = "";
char *password = "a.123456987";
char *phone = "18712345678";
//必须带签名
char *msg = "【云通讯】您的验证码是123400";
//get_balance(account, password);
send_sms(account, password, phone, msg);
close(sockfd);
exit(0);
}