C++对接语音验证码接口DEMO示例

C++对接语音验证码接口DEMO示例,第1张

C++对接语音验证码接口DEMO示例
//接口类型:互亿无线语音验证码接口。
// 账户注册:请通过该地址开通账户http://user.ihuyi.com/?h9Dkn
// 注意事项:
//(1)调试期间,请仔细阅读接口文档;
//(2)请使用 APIID 及 APIKEY来调用接口,可在会员中心获取;
//(3)该代码仅供接入互亿无线短信接口参考使用,客户可根据实际需要自行编写;
 
// DEMO仅作参考
#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

extern int h_errno;

int basefd;
char *hostname = "api.voice.ihuyi.com";
char *send_sms_uri = "/webservice/voice.php?method=Submit&format=json";


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.0rn"
        "Host: %srn"
        "Content-type: application/x-www-form-urlencodedrn"
        "Content-length: %zurnrn"
        "%s", page, hostname, strlen(poststr), poststr);

    write(basefd, sendline, strlen(sendline));
    while ((n = read(basefd, recvline, MAXLINE)) > 0) {
        recvline[n] = '';
        printf("%s", recvline);
    }
    return n;
}


ssize_t send_sms(char *account, char *password, char *mobile, char *content)
{
    char params[MAXPARAM + 1];
    char *cp = params;
    sprintf(cp,"account=%s&password=%s&mobile=%s&content=%s", account, password, mobile, content);
    return http_post(send_sms_uri, cp);
}

int  socked_connect(char *arg)
{
    struct sockaddr_in their_addr = {0};  
    char buf[1024] = {0};  
    char rbuf[1024] = {0};  
    char pass[128] = {0};  
    struct hostent *host = NULL;   
    
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd<0)
    {
        printf ("create the sockfd is failedn");
        return -1;
    }
    
    if((host = gethostbyname(arg))==NULL)  
    {  
        printf("Gethostname error, %sn");  
        return -1; 
    }  
 
    memset(&their_addr, 0, sizeof(their_addr));  
    their_addr.sin_family = AF_INET;  
    their_addr.sin_port = htons(80);  
    their_addr.sin_addr = *((struct in_addr *)host->h_addr);
    if(connect(sockfd,(struct sockaddr *)&their_addr, sizeof(struct sockaddr)) < 0)  
    {  
        close(sockfd);
        return  -1;
    }  
    printf ("connect is successn");
    return sockfd;
    
}

int main(void)
{
    struct sockaddr_in servaddr;
    char str[50];
    
    #if 0
    //建立socket连接
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_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));
    #endif
    
    if((basefd= socked_connect(hostname))==-1)
    {
        printf("connect is failedn");
        return -1;
    }
    printf("basefd is =%dn",basefd);
    //查看用户名 登录用户中心->语音验证码>产品总览->API接口信息->APIID
    char *account = "用户名";

    //查看密码 登录用户中心->语音验证码>产品总览->API接口信息->APIKEY
    char *password = "密码";

    //手机号
    char *mobile = "138xxxxxxxx";

    //短信内容
    char *message = "1125";

    
    send_sms(account, password, mobile, message);
    printf("send the message is successn");
    close(basefd);
    exit(0);
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4752007.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-08
下一篇 2022-11-08

发表评论

登录后才能评论

评论列表(0条)

保存