C++调取手机短信验证码_C/C++_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > C/C++ > C++调取手机短信验证码

C++调取手机短信验证码

 2018/3/29 21:01:20  创蓝253  程序员俱乐部  我要评论(0)
  • 摘要:[C++]基于创蓝253云通讯paas平台c/c++短信接口demo【仅供参考】#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<
  • 标签:手机 c++
[C++] 基于创蓝253云通讯paas平台c/c++短信接口 demo  【仅供参考】

#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


//线上线下接口宏开关

#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";



/**
* * 发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.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] = '\0';
        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);
}



上一篇: Java获取短信验证码接口调用demo 下一篇: 没有下一篇了!
发表评论
用户名: 匿名