#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netdb.h>
#include <unistd.h>
#define SA struct SOCKADDR
#define MAXLINE 4096
#define MAXSUB 2000
#define MAXPARAM 2048
#define LISTENQ 1024
Online and offline interface macro switch
#define ONLINE
extern int H_errno;
int sockfd;
Char hostname = "123.59.105.84";
char Send_sms_uri = "/msg/send";
Char *query_balance_uri = "/msg/balance";
/**
-
- Send an HTTP POST request
- /
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.0\r\n"
"Host:sms.253.com\r\n"
"Content-type:application/x-www-form-urlencoded\r\n"
"Content-length:%zu\r\n\r\n"
"%s", page, strlen (POSTSTR), poststr);
Write (SOCKFD, Sendline, strlen (Sendline));
printf ("\n%s", sendline);
printf ("\ n--------------------------\ n");
while ((n = Read (SOCKFD, Recvline, MAXLINE)) > 0) {
Recvline[n] = ' + ';
printf ("%s\n", recvline);
}
return n;
}
/**
-
/
ssize_t get_balance (char un, char PW)
{
Char Params[maxparam + 1];
char CP = params;
sprintf(cp,"un=%s&pw=%s", un, pw);
Return Http_post (Query_balance_uri, CP);
}
/**
-
/
ssize_t send_sms (char un, char pw, char phone, char msg)
{
Char Params[maxparam + 1];
char CP = params;
sprintf(cp,"un=%s&pw=%s&phone=%s&msg=%s&rd=1", un, pw, 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 *un = "账号"; char *pw = "密码"; char *phone = "手机号"; //必须带签名 char *msg = "【253云通讯】您的验证码是123400";//get_balance(un, pw);send_sms(un, pw, phone, msg);close(sockfd);exit(0);
}
C + + SMS Interface Demo: Create blue 253 platform SMS Verification Code interface, SMS Marketing interface