Example of accessing webservice in C language: webservice in C Language

Source: Internet
Author: User

Example of accessing webservice in C language: webservice in C Language

In a recent project, you need to access webservice to obtain some json data. The returned page content is only json data, not as complex as the actual page. So I don't want to introduce the tool library any more and simply implement it using socket.

The following code is not the actual project Source Code. It is a small example I extracted to share with you. The code itself is easy to understand, but it requires some socket programming and http protocol basics.

# Include <stdio. h> # include <sys/socket. h> # include <netinet/in. h> # include <errno. h> # include <time. h> int main () {int sockfd = 0; struct sockaddr_in ser_addr; char url [1024] = {0}; // Request Buffer char recvbuff [4096] = {0 }; // receives the buffer char * data = NULL; struct timeval time = {5, 0}; int ret = 0; // fills in the webservice host information memset (& ser_addr, 0, sizeof (ser_addr); ser_addr.sin_family = AF_INET; ser_addr.sin_port = hto Ns (80); ret = inet_ton (AF_INET, "127.0.0.1", & ser_addr.sin_addr.s_addr); if (ret <0) {perror ("ERROR"); return-1 ;} if (ret = 0) {puts ("ERROR: arguments error"); return-1 ;}// create socket sockfd = socket (AF_INET, SOCK_STREAM, 0 ); if (sockfd <0) {perror ("ERROR"); return-1 ;}// set the blocking time to 5 s ret = setsockopt (sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char *) & time, sizeof (time); if (ret <0) {perror ("E RROR "); goto end;} // connect to webservice ret = connect (sockfd, (struct sockaddr *) & ser_addr, sizeof (struct sockaddr); if (ret! = 0) {perror ("ERROR"); goto end;} // fill other attributes in the request url header and add them by yourself. End snprintf (url, sizeof (url), "GET/test HTTP/1.0 \ r \ n") with a line break and press Enter "); // initiate an http request ret = send (sockfd, url, strlen (url), 0); if (ret <0) {perror ("ERROR"); goto end ;} puts ("send success"); // The page returns ret = recv (sockfd, recvbuff, sizeof (recvbuff), 0); if (ret <0) {perror ("ERROR"); goto end;} puts ("recv success"); // parse the returned page and point the data pointer to the data subject data = strstr (recvbuff, "\ r \ n"); data + = 4; // print the page if (data! = NULL) {puts (data);} end: close (sockfd); return 0 ;}

 

Related Article

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.