Http Post in C

M

Mike Chirico


#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>

#define SA struct sockaddr
#define MAXLINE 4096

/* Following could be derived from SOMAXCONN in <sys/socket.h>, but many
kernels still #define it as 5, while actually supporting many more */
#define LISTENQ 1024 /* 2nd argument to listen() */



void process_http(FILE *fp, int sockfd){
char sendline[MAXLINE], recvline[MAXLINE];
ssize_t n;

strcpy(sendline,"POST /test.php HTTP/1.0\r\n");
strcat(sendline,"Host: souptonuts.sourceforge.net\r\n");
strcat(sendline,"Content-type: application/x-www-form-urlencoded\r\n");
strcat(sendline,"Content-length: 34\r\n\r\n");
strcat(sendline,"mode=login&user=test&password=test\r\n");


write(sockfd, sendline, strlen(sendline));
while ( ( n = read(sockfd, recvline, MAXLINE)) != 0 )
{
recvline[n]='\0';
fprintf(stderr,"%s",recvline);
}

/* Interesting note: if you want to see how many
reads there are through the while loop do a

# tcpdump src port 80

And the size of n will be the mss, value in ( ) from this
call

*/


}





int
main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr;



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, "66.35.250.209", &servaddr.sin_addr);

connect(sockfd, (SA *) &servaddr, sizeof(servaddr));
process_http(stdin, sockfd);
exit(0);

}


Mike Chirico
 
K

Kevin Goodsell

Mike said:
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>

<snip>

This group is for discussion of the C language as defined by the
international C standard. Please don't post non-standard code here. Only
5 of those headers are standard.

This is also not a source code repository, so don't post code just to be
posting code. Only do it if you have a question or comment (relating to
the C language) about the code.

-Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,303
Messages
2,571,557
Members
48,359
Latest member
Raqib_121635
Top