Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Issuing an http request via c 1

Status
Not open for further replies.

yudazdk

Programmer
Oct 8, 2006
2
0
0
IL
Hello

OS: RedHat Linux

I have to write a c program that should issue an HTTP GET request such as the following:

localhost/seo/test_tal.php?val=2 (The script updates a row
in the database table)

I tried using socket for that on port 80.
I used functions:
1) sockfd = socket(PF_INET, SOCK_STREAM, 0)
2) connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct
sizeof(struct sockaddr))
3) bytes_sent = send(sockfd, msg, len, 0) ; // msg contains the http
headers get request

The code:

Code:
void http_socket() {
   int sockfd ;
   struct sockaddr_in dest_addr ;
   int len, bytes_sent ;
   char *msg = malloc(MAXDATASIZE) ; 
   
   // do some error checking!
   if (sockfd = socket(PF_INET, SOCK_STREAM, 0) == ERROR_NUMBER) {
      printf("Socket error\n") ;
      exit(1) ;
   }
   
   dest_addr.sin_family = AF_INET ;         // host byte order
   dest_addr.sin_port = htons(MYPORT) ;     // short, network byte order, MYPORT=80
   dest_addr.sin_addr.s_addr = inet_addr(DEST_IP) ;
   memset(&(dest_addr.sin_zero), '\0', 8) ; // zero the rest of the struct

   // don't forget to error check the connect()!
   if ( connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr)) ==           ERROR_NUMBER) {
      exit(1) ;
   }   
   bytes_sent = 0 ;
   
   strcpy(msg, "[URL unfurl="true"]http://localhost/seo/test_tal.php?val=2[/URL] ") ;
   //strcat(msg, "HTTP/1.0") ;
   len = strlen(msg) ;
   bytes_sent += send(sockfd, msg, len, 0) ;   
      
   printf("Bytes sent = %i\n", bytes_sent) ;
   
   free(msg) ;
}

The problem is that the bytes are sent but the script test_tal.php
is not activated.

Thanks
Yehuda
 
1. Get 2. Use ethereal to observe network traffic
3. Use your normal browser to issue the request, and examine the ethereal capture to study the protocol.
4. Issue the same request from your program and observe that as well. Adjust your program until it matches what your browser does.

An alternative is to use say which takes care of a lot of the low-level HTTP protocol issues for you.

--
 
Is there a library in c for curl ?

Regards
 
Yes - libcurl - read the site - you probably already have it.


--
 
Its probably better to use an HTTP lib as Salem says, but ...

Your original request is not compliant to the HTTP protocol.

Here is one that is :

Code:
char* szResource = "/seo/test_tal.php?val=2";

sprintf(szHttpRequest, "GET %s HTTP/1.1\nHost:%s\nUser-Agent: My HTTP Test Agent\nAccept: text/html,text/plain,image/png,image/jpeg,image/gif\nAccept-Language: en-us,en\nAccept-Encoding: gzip,deflate\nAccept-Charset: ISO-8859-1\nKeep-Alive: 300\nConnection: keep-alive\n\n", szResource);

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top