返回顶部

收藏

发送http请求的C++代码实现

更多
    #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  

    extern int h_errno;  

    int sockfd;  
    char *hname = "yunpian.com";  
    char *send_sms_json = "/v1/sms/send.json";  
    char *get_user_json = "/v1/user/get.json";  

    /** 
     * 发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: %s\r\n"  
                 "Content-type: application/x-www-form-urlencoded\r\n"  
                 "Content-length: %zu\r\n\r\n"  
                 "%s", page, hname, strlen(poststr), poststr);  

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

    /** 
     * 查账户信息 
     */  
    ssize_t get_user(char *apikey)  
    {  
        char params[MAXPARAM + 1];  
        char *cp = params;  
        sprintf(cp,"apikey=%s", apikey);  
        return http_post(get_user_json, cp);  
    }  

    /** 
     * 使用通用接口发短信 
     */  
    ssize_t send_sms(char *apikey, char *mobile, char *text)  
    {  
        char params[MAXPARAM + 1];  
        char *cp = params;  
        sprintf(cp,"apikey=%s&amp;mobile=%s&amp;text=%s", apikey, mobile, text);  
        return http_post(send_sms_json, cp);  
    }  

    int main(void)  
    {  
        struct sockaddr_in servaddr;  
        char **pptr;  
        char str[50];  
        struct hostent *hptr;  
        if ((hptr = gethostbyname(hname)) == NULL) {  
            fprintf(stderr, "通过域名获取IP失败: %s: %s",  
                    hname, hstrerror(h_errno));  
            exit(1);  
        }  
        printf("域名: %s\n", hptr->h_name);  
        if (hptr->h_addrtype == AF_INET  
            &amp;&amp; (pptr = hptr->h_addr_list) != NULL) {  
            printf("IP: %s\n",  
                   inet_ntop(hptr->h_addrtype, *pptr, str,  
                             sizeof(str)));  
        } else {  
            fprintf(stderr, "Error call inet_ntop \n");  
            exit(1);  
        }  

        //建立socket连接  
        sockfd = socket(AF_INET, SOCK_STREAM, 0);  
        bzero(&amp;servaddr, sizeof(servaddr));  
        servaddr.sin_family = AF_INET;  
        servaddr.sin_port = htons(80);  
        inet_pton(AF_INET, str, &amp;servaddr.sin_addr);  

        connect(sockfd, (SA *) &amp; servaddr, sizeof(servaddr));  

        //修改为您的apikey  
        char *apikey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";  
        //修改为您要发送的手机号  
        char *mobile = "188xxxxxxxx";  
        //设置您要发送的内容  
        char *text = "您的验证码是1234";  

        /**************** 查账户信息调用示例 *****************/  
        get_user(apikey);  

        /**************** 使用通用接口发短信 *****************/  
        //send_sms(apikey, mobile, text);  

        close(sockfd);  
        exit(0);  
    }             

标签:c/c++

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. yuer 发表 2018-07-27 08:46:07 coredump之百米之内必有解药
  2. hev 发表 2018-04-28 06:11:38 一个简单、轻量的 Linux 协程实现
  3. hev 发表 2017-10-19 15:56:11 FSH – 助你接入私有网络中的 Linux 终端
  4. gonwan 发表 2015-04-15 08:03:07 Database Access Layer in C++
  5. gonwan 发表 2015-12-28 08:41:13 Basic Usage of Boost MultiIndex Containers
  6. gonwan 发表 2016-01-19 03:37:54 Coroutines in C++/Boost
  7. Haoxiang Li 发表 2017-10-25 20:29:02 MXNet C++ Deployment
  8. yuer 发表 2017-10-20 07:52:47 基于leveldb的持久消息队列SDK
  9. yuer 发表 2017-10-07 07:51:32 c++11完美转发
  10. 博主 发表 2016-09-03 00:00:00 C++编译期类型信息的利用
  11. yuer 发表 2017-09-06 03:03:29 libcurl访问unix socket
  12. yuer 发表 2017-09-07 08:14:58 valgrind检测php扩展的warning