How can I post data with POST method in C programming.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Your code will not work if the site that you wish to log onto
uses cookies in the validation process of logging in.
As a side note,
As a HTTP/1.1 implementation your code should be able to
handle HTTP re-directs,i.e status 30x return codes.
Also all HTTP/1.1 implementations should also be able to
handle the chunked transfer-encoding scheme
when retrieving documents which means that to be able to
properly retrieve such encoded docs you can't just read from
the socket until an error or EOF is seen.
The encoding scheme is not differcult but you need to be aware of it.
I re-direct your attention again to the HTTP/1.1 rfc
:0)
Anyway here is a successful POST / request capture using
cookies.
ta
If the case for loging in involve cookie, it shouldn't be so hard.
When server reply (as you can see in capture header, you can use Proxomitron in windows to capturing the header too), it will include "Set-Cookie:" header section that tell the piece of data of the loging in session.
So you can use this data to access other page that require it by request the file you want on the server side with 'Cookie: username=something; PHPSESSID=somthing\n" include in your header.
And you can see the authorized page.
As for redirect, maybe we just read the header sending back from the server that tell us to redirect to other page, and then call the process for connecting to that server again, if that server reply with redirect again we follow the same again.
For read the encode data, we must see http's rfc as per slzckboy said. TO see the method for encoding and decoding (of course the sender that encoding data want receiver to see the actual data so the decoding method should exist).
TO this point I think, we now can code get, post method and read header from server also sending header and data to server, i think we can create our own Proxy Program hmm!
slzckboy what program do you use to capture that header, is it on Linux? I am trying to find that kind of program except SQUID to run on linux for a while now.
As i said about calculating size of post data, forget what I said this is the correct one.
Example
'username=user1&password=pass1' the size is 30, that is you fill in the http header with 'Content-Type: 30'.
REMEMBER calculate the length of string data and PLUS 1 for null-terminate.
I just used strlen() to calculate the entity length.
The server is not going to be looking for the null terminator,its just going to read Content-Length bytes.
Thats what the Content-Length field is for.
This is how I built my header.
I have been able to login to a number of sites that I have a membership with ok.
Code:
......
#define POSTENTITY \
"username=%s&password=%s"
......
#define POSTHEADER \
"POST %s HTTP/1.1\r\nHost: %s\r\nConnection:"\
" keep-alive\r\nAccept: text/html\r\nAccept: image/gif\r\nAccept: image/x-xbitmap\r\n"\
"Content-Type: application/x-www-form-urlencoded\r\nContent-length: %d\r\n"\
"%s"\ /* Cookie header field */
"Accept: image/jpeg\r\nAccept: image/png\r\n\r\n"\
"%s" /*entity*
..................
int login(POST *post){
if((snprintf(post->entity,MAX,POSTENTITY,post->username,post->password)>=MAX))
return -1;
else
return 0;
}
...................
if((snprintf(sendbuff,MAX,POSTHEADER,head->resource,head->dns_name,(int)strlen(head->entity),head->cookie,head->ent
ity) >=MAX)){
puts("WARNING:Outputbuffer too small for last write");
return -1;
}
I have to agree,it was tricky in C although i did it as a
learning aid really to know more about www,C programming etc etc..
If your serious about it you have to get to grips with the
HTTP RFC first,which is unfortunately a bit boring,but it makes life easier in the long run.
The basics of it it easy enough,just reading and writing to a socket.
Then its just message parsing.
There's a whole lot of parsing strings.
C dosn't give you much help with this sort of thing.
<thinking out loud>
I think I'm going to learn a new language soon....
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.