//接口類型:短信接口
//測試環(huán)境:centos7.8 gcc4.8.5
//測試日期:2022-01-26
//使用說明:demo只包含了對接時需要的傳參和解析的核心代碼,可參考整合到自己的系統(tǒng)中,具體業(yè)務(wù)細(xì)節(jié)代碼可根據(jù)自己的需要進(jìn)行調(diào)整及優(yōu)化。
//linux下的編譯:gcc -o test ./test.c
//linux下的執(zhí)行:./test
//接口類型:互億無線觸發(fā)短信接口,支持發(fā)送驗(yàn)證碼短信、訂單通知短信等。
//賬戶注冊:請通過該地址開通賬戶http://sms.ihuyi.com/register.html
//注意事項(xiàng):
//(1)調(diào)試期間,請用默認(rèn)的模板進(jìn)行測試,默認(rèn)模板詳見接口文檔;
//(2)請使用APIID(查看APIID請登錄用戶中心->驗(yàn)證碼短信->產(chǎn)品總覽->APIID)及APIkey來調(diào)用接口;
//(3)該代碼僅供接入互億無線短信接口參考使用,客戶可根據(jù)實(shí)際需要自行編寫;
#include
#include <sys/socket.h>
#include <sys/types.h>
#include
#include
#include
#include
#include
#include
#include <sys/wait.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include
#define host "106.ihuyi.com"
#define PORT 80
#define BUFSIZE 4096
int main(int argc, char **argv)
{
int sockfd, ret, i, h,srandnum;
struct sockaddr_in servaddr;
char str1[4096], str2[4096], buf[BUFSIZE], *str;
socklen_t len;
fd_set t_set1;
struct timeval tv;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) { //創(chuàng)建套接字
printf("create connect error!\n"); //創(chuàng)建網(wǎng)絡(luò)連接失敗
exit(0);
};
bzero(&servaddr, sizeof(servaddr)); //每個字節(jié)都用0填充
servaddr.sin_family = AF_INET; //使用IPv4地址
servaddr.sin_port = htons(PORT); //端口
if (inet_aton(host, &servaddr.sin_addr) == 0){
struct hostent *he;
he = gethostbyname(host);
if (he == NULL)
return -1;
memcpy(&servaddr.sin_addr, he->h_addr, sizeof(struct in_addr));
}
char ipbuf[128];
strncpy(ipbuf, inet_ntoa(servaddr.sin_addr), 128); //將域名轉(zhuǎn)成IP
if (inet_pton(AF_INET, ipbuf, &servaddr.sin_addr) <= 0 ){
printf("inet_pton error!\n"); //創(chuàng)建網(wǎng)絡(luò)連接失敗
exit(0);
};
if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0){
printf("connect error!\n");
exit(0);
}
printf("connect success!\n\n");
//發(fā)送數(shù)據(jù)
memset(str2, 0, 4096);
strcat(str2, "account=xxxxxxxx&password=xxxxxxxxxxxx&mobile=139xxxxxxxx&content=您的驗(yàn)證碼是:7328,請不要把驗(yàn)證碼泄露給其他人。");
str=(char *)malloc(128);
len = strlen(str2);
sprintf(str, "Content-Length: %d\r\n\r\n", len);
memset(str1, 0, 4096);
strcat(str1, "POST /webservice/sms.php?method=Submit&format=json HTTP/1.1\r\n");
strcat(str1, "Host: ");
strcat(str1, host);
strcat(str1, "\r\n");
strcat(str1, "Content-Type: application/x-www-form-urlencoded\r\n");
strcat(str1, str);
strcat(str1, str2);
strcat(str1, "\r\n\r\n");
printf("Request Data: \n%s\n",str1);
ret = write(sockfd,str1,strlen(str1));
if (ret < 0) {
printf("send data fail!errno:%d, errmsg:'%s'\n\n",errno, strerror(errno));
exit(0);
}else{
printf("send data success, length:%d byte!\n\n", ret);
}
FD_ZERO(&t_set1);
FD_SET(sockfd, &t_set1);
while(1){
//sleep(1);
tv.tv_sec= 0;
tv.tv_usec= 0;
h = select(sockfd+1, &t_set1, NULL, NULL, &tv);
printf("h:%d sec:%ld usec:%ld\n\n", h, tv.tv_sec, tv.tv_usec);
if (h == 0) {
memset(buf, 0, 4096);
i= read(sockfd, buf, 4095);
if (i==0){
close(sockfd);//關(guān)閉套接字
printf("0.connect close!\n"); //讀取數(shù)據(jù)報(bào)文時發(fā)現(xiàn)遠(yuǎn)端關(guān)閉
return -1;
}
printf("Response Data: \n%s\n", buf);
close(sockfd);//關(guān)閉套接字
printf("1.connect close!\n");
return 1;
}
if (h > 0) {
close(sockfd);//關(guān)閉套接字
printf("2.connect close!\n");
return -1;
};
if (h < 0) {
close(sockfd);//關(guān)閉套接字
printf("3.connect close!\n"); //在讀取數(shù)據(jù)報(bào)文時SELECT檢測到異常
return -1;
};
//continue;
//break;
}
close(sockfd);//關(guān)閉套接字
printf("4.connect close!\n");
return 0;
}
驗(yàn)證碼已發(fā)送到您的手機(jī),請查收!
輸入驗(yàn)證碼后,點(diǎn)擊“開通體驗(yàn)賬戶”按鈕可立即開通體驗(yàn)賬戶。