By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,852 Members | 2,219 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,852 IT Pros & Developers. It's quick & easy.

Http Post in C

P: n/a
For a C http_post example ref:

http://souptonuts.sourceforge.net/code/http_post.c.html

Regards,

Mike
Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
mc******@brainsquash.org (Mike Chirico) wrote in message news:<ad**************************@posting.google. com>...
For a C http_post example ref:

http://souptonuts.sourceforge.net/code/http_post.c.html

Regards,

Mike


#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=tes t\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
Nov 13 '05 #2

P: n/a
Mike Chirico wrote:
mc******@brainsquash.org (Mike Chirico) wrote in message news:<ad**************************@posting.google. com>...
For a C http_post example ref:

http://souptonuts.sourceforge.net/code/http_post.c.html

Regards,

Mike

#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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Nov 13 '05 #3

P: n/a
mc******@brainsquash.org (Mike Chirico) wrote:
mc******@brainsquash.org (Mike Chirico) wrote:
For a C http_post example ref:

http://souptonuts.sourceforge.net/code/http_post.c.html

#include <arpa/inet.h>

<snip>
Stop right here! What made you think that C code littered with
non-standard extensions to the language could ever be topical in
comp.lang.c?

Please read the c.l.c welcome-message

http://www.angelfire.com/ms3/bchambl...me_to_clc.html

and the c.l.c-faq

http://www.eskimo.com/~scs/C-faq/top.html

before posting here. Thank you very much.

BTW, what was your question?

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.