How can I post data with POST method in C programming.
I extreamly want to how can we program in C code to perform post data (such as login to some website which we send the username and pass data to web server, or any type of this kind which involve POST method).
I have try to program with GET method, it's work fine. Also i search through the net all day today, but i found info that can't immediately go into the real situaion here. Below is C Code with GET Method, you can try with # ./httpgetconnect <IPaddress> <relative_url> => ./httpgetconnect 210.1.13.198 / this result will print the content in html to STDOUT Please save from this link: [HTML]http://210.1.13.198/haxpor/httpgetconnect[/HTML] the source is below. Code:
#include <stdio.h> Any suggesstion that the code would be modify to "Perform POST Method" would be appreciate. I guess it might be something about header. Thanks. NOTE mynetwork.h is nothing special, it just include socket and relate header files and it contained err_exit() function only. |
Hi
I don't know, is this for learning or a practical issue? Anyway, why not use libcurl, wget, or some other tool? If it's for learning, why not look at the source? What I really like about opensource is the huge amount of examples - I have source code for everything :) |
I also download curl to my computer not long ago, but I go into it search through the source but I cant found the API function that perform for Post Method in curl. Maybe I miss something but I try it so long to keep finding it.
Maybe you can show me that section of sourcecode then (if you have time) :) Thank very much. It's great to be helped. |
Your best bet is to read the HTTP rfc to see how to properly form a POST request.
Once you understand the protocol properly you can then translate this to C code. I have done a bit of http/https programing and that is how I approach it. http://www.w3.org/Protocols/rfc2616/rfc2616.html |
An example of libcurl HTTP post:
http://curl.haxx.se/lxr/source/docs/examples/postit2.c |
slzckboy, do you have some sample sourcecode (POST Method) if then, could you show me right away.
I really want it. This can save my time alot and also the others who want to know too. Thanks in advance. |
After a long time for trying and experimenting the code with c. I decide to see the header in http which sending between my computer and server (that trap with Proxomitron 4.5 in Windows), I take that info and adapt it then try it with perl (usually very close with C), and it work with POST Method.
The sourcecode for Perl which connect to Any web server with POST Method is below. Code:
#!/usr/bin/perl -w Note You must fill in the infomation by yourself with that bold string(url of web server, data to send, etc) Also be careful with the line that I commented, not include '&' in calculation so you have your data plus 1 byte to fill in Content-Length: header. _Example_ <LINK> => /member.php?act=login <FullUrlLink> => http://www.something.org After this perl code is work fine, then I jump it concept to C immediately but I found it wont work. Any suggestion? |
what is the server replying with?
are theyjust closing the connection straight away,or are the sending a http response code if so what code. All my http code is for the GET method,but I will have a go at figuring it out with you. Is this to interact with cgi programs on servers;i.e for login info ? |
Yes, this interact with CGI script hosted on the web server.
The server return with 400 Bad Request code, still now I figuring it out what my header in C code is wrong. I do change it to be the same as Perl code above and modify it a little but the result is same it return 400 code every time. IT MUST BE WRONG WITH HEADER, i guess that, other part is fine. I am sure you have some account with some web server, if possible you can test this thing with the C Code above that i post but change the line -> "GET %s .... to 'POST header' in Perl code above and run it with web server ip. Thanks again. |
one thing i notice about your header is that it is non
standard in that you are suppose to have \r\n between each header element as per the rfc This is the header I use. Note:you should terminate your header with \r\n\r\n Code:
|
thanks for that point.
I fix as per slzckboy suggest but it still wont work with POST Method. Keep it coming, i calmly waiting. Thanks again. |
OK i will make it more comfortable for you all who trying to help.
You can download the sourcecode in .tar package (include main source and header file) for httpgetconnect, for this you can modify it to test with POST Method. http://210.1.13.198/haxpor/httpgetconnect.tar Hope this will provide more convenient. |
I't would be more helpful if you Just post the C code section relevant to
the POST request. You havn't posted that yet. Only you GET code. thnx |
In fact the POST code is the same as GET code but instead you must change the HEADER section of the line 'sprint("GET...' to POST Specific header.
But as you wish, Below is the code for POST Method. Code:
#include <stdio.h> This is the link for httppostconnect (include header fies and sourcecode) http://210.1.13.198/haxpor/httppostconnect.tar |
Ok everyone, i want to tell you all that the problem is the HEADER as I suggest, and it is true.
It is my mistake not to calmly and slowly see at the header. The header is wrong. As you can see from GET Method code from above the line 'sprintf(..' you will see "GET %s HTTP1.1.." this is wrong, the correct is "GET %s HTTP/1.1". Although that mistake work with GET Method but it wont work with POST Method. So after all, you can try this by correct that line of header to the correct one as i tell you, then run it. (NOTE: you must fill in the info of the server and data you want to send by yourself) Tell me if it work! PS. -Thanks to you all that work along and together figure it out the problem. Guttorm, slzckboy. |
All times are GMT -5. The time now is 05:39 PM. |