Code implementation for sending http requests and sending Request Code

Source: Internet
Author: User

Code implementation for sending http requests and sending Request Code

# 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 ";/*** send http post request */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 "" % s ", page, hna Me, strlen (poststr), poststr); write (sockfd, sendline, strlen (sendline); while (n = read (sockfd, recvline, MAXLINE)> 0) {recvline [n] = '\ 0'; printf ("% s", recvline);} return n ;} /*** query 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);}/*** send a text message using the common interface */ssize_t send_sms (char * apikey, char * m Obile, 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, "failed to get IP through Domain Name: % 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 a socket connection sockfd = socket (AF_INET, SOCK_STREAM, 0 ); bzero (& servaddr, sizeof (servaddr); servaddr. sin_family = AF_INET; servaddr. sin_port = htons (80); inet_ton (AF_INET, str, & servaddr. sin_addr); connect (sockfd, (SA *) & servaddr, sizeof (servaddr); // modify it to your apikey char * apikey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx "; // change the mobile phone number to char * mobile = "188 xxxxxxxx"; // set the content to be sent to char * text = "your verification code is 1234 "; *****************/ get_user (apikey ); *****************// /send_sms (apikey, mobile, text); close (sockfd); exit (0 );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.