發送http請求的代碼實現,發送請求代碼

來源:互聯網
上載者:User

發送http請求的代碼實現,發送請求代碼

#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         1024extern 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&mobile=%s&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        && (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(&servaddr, sizeof(servaddr));    servaddr.sin_family = AF_INET;    servaddr.sin_port = htons(80);    inet_pton(AF_INET, str, &servaddr.sin_addr);        connect(sockfd, (SA *) & 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);}           

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.