"C (c + +)" Gen Blue 253 Cloud communication platform International SMS API interface Demo

Source: Internet
Author: User
Tags http post sprintf

#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 <accountistd.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 = "118.178.16.150";
char
Send_sms_uri = "/send/json";
Char *query_balance_uri = "/balance/json";

/**

      • 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.1\r\n"
      "Host:intapi.253.com\r\n"
      "Content-type:application/json\r\n"
      "Content-length:%zu\r\n\r\n"
      "%s", page, strlen (POSTSTR), poststr);
      , page, 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;
      }

/**

      • Balance of Audit account
    • /
      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);
      }

/**

      • Send SMS
    • /
      ssize_t send_sms (char
      account, char password, char mobile, char msg)
      {
      Char Params[maxparam + 1];
      char
      CP = params;

      sprintf (cp, "{\" account\ ": \"%s\ ", \" password\ ": \"%s\ ", \" mobile\ ": \"%s\ ", \" msg\ ": \"%s\ "}", account, password, mobile , 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";//手机号码,格式(区号+手机号码),例如:8615800000000,其中86为中国的区号char *mobile = "8615800000000";//必须带签名char *msg = "【253云通讯】您的验证码是123400";//get_balance(account, password);send_sms(account, password, mobile, msg);close(sockfd);exit(0);

}

"C (c + +)" Gen Blue 253 Cloud communication platform International SMS API interface Demo

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.