#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 1024ext ern int h_errno;int Sockfd;char *hname = "yunpian.com"; char *send_sms_json = "/v1/sms/send.json"; char *get_user_json = "/V 1/user/get.json ";/** * Send HTTP POST request */ssize_t http_post (char *page, char *poststr) {char sendline[maxline + 1], Recvlin E[maxline + 1]; ssize_t N; snprintf (Sendline, Maxsub, "POST%s http/1.0\r\n" "Host:%s\r\n" "Content-type:applic Ation/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] = ' + '; printf ("%s", recvline); } return n;} /** * Check account information */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);} /** * SMS */ssize_t send_sms (char *apikey, Char *mobile, char *text) using generic interface {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, "Get IP through Domain name failed:%s:%s", Hname, Hstrerror ( H_errno)); Exit (1); } printf ("Domain name:%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); }//Establish socket connection 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)); Modify for your apikey char *apikey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; Modify the phone number that you want to send char *mobile = "188xxxxxxxx"; Set the content you want to send char *text = "Your captcha is 1234"; /**************** account Information Call example *****************/Get_user (Apikey); /**************** SMS *****************///send_sms (apikey, Mobile, text) using the Universal interface; Close (SOCKFD); Exit (0);}
Code implementation to send HTTP requests