Linux under C language URL request

Source: Internet
Author: User

Any code only you write, can understand deeply, sometimes look at the document look at a very vacant time, try to find some other people's source code, in other people's source changes, in debugging, may achieve a multiplier effect.

Recently, the project needs to access a URL interface in Linux to pass parameters and get the return value, see a lot of methods, know with the socket, but did not touch, feel very vacant, on the Internet to find instances, see, have a little feeling, and in the case of others after the changes can be used, At that time stuck in the HTTP format, write a demo below, as long as the URL in my code to change, it will certainly succeed. Note The URL does not/HTTP/

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>


#define BUFSIZE 0xf000
void Geturl (char* URL)
{
int CFD;
struct sockaddr_in cadd;
struct Hostent *purl = NULL;
Char Myurl[bufsize];
char *phost = 0;
Char Host[bufsize],get[bufsize];
Char Request[bufsize];
static Char text[bufsize];
int i,j;


Detaching host addresses and relative paths from a host
memset (myurl,0,bufsize);
memset (host,0,bufsize);
memset (get,0,bufsize);
strcpy (Myurl,url);
for (Phost = myurl;*phost! = '/' && *phost! = '; ++phost);


Get relative path saved to get
if ((int) (phost-myurl) = = strlen (myurl))
{
strcpy (GET, "/");//That is, the URL does not give a relative path, you need to manually at the end of the URL
Department Plus/
}
Else
{
strcpy (get,phost);//Address segment Phost to strlen (Myurl) is a relative path saved
}

Save host information to host
This resets it to zero, that is, the content that it points to has been separated from the relative path, the rest is the host letter
(from Myurl to Phost address segment is host)
*phost = ' + ';
strcpy (Host,myurl);
Setting the socket parameter
if ( -1 = = (cfd = socket (af_inet,sock_stream,0)))
{
printf ("Create socket failed of client!\n");
Exit (-1);
}

PURL = gethostbyname (host);//Get domain > Name information through the domain name resolution function of the host information obtained above

Set the IP address structure
Bzero (&cadd,sizeof (struct sockaddr_in));
cadd.sin_family = af_inet;
CADD.SIN_ADDR.S_ADDR = * ((unsigned long*) purl->h_addr_list[0]);
Cadd.sin_port = htons (80);
Send URL information to the Web server
memset (request,0,bufsize);
strcat (Request, "GET");
strcat (Request,get);
strcat (Request, "http/1.1\r\n");//This is the information for the HTTP request line
strcat (Request, "HOST:");
strcat (Request,host);
strcat (Request, "\ r \ n");
strcat (Request, "cache-control:no-cache\r\n\r\n");
Connecting to a server

int cc;
if ( -1 = = (cc = Connect (CFD, (struct sockaddr*) &cadd, (socklen_t) sizeof (CADD))))
{
printf ("Connect failed of client!\n");
Exit (1);
}
printf ("Connect success!\n");

Request to send URL requests to the server
INT CS;
if ( -1 = = (cs = Send (Cfd,request,strlen (request), 0)))
{
printf ("Request failed to send requests to the server!\n");
Exit (1);
}
printf ("Send successfully, Bytes Sent:%d\n", CS);

Client receives the return information of the server
memset (text,0,bufsize);
int CR;
if ( -1 = = (CR = recv (cfd,text,bufsize,0)))
{
printf ("recieve failed!\n");
Exit (1);
}
Else
{
printf ("Receive succecc!\n");
}
Close (CFD);
}

int main (int argc,char* argv[])
{
if (argc<2)
{
printf ("Usage:%c URL page URL \ n", argv[0]);
Exit (1);
}
Geturl (argv[1]);
return 0;
}


Linux under C language URL request

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.