CGIC库之get与post请求一
get请求demo:
login.html文件,放到/boa/www/ 下
<html> <head> <title>Test</title> </head> <body> <form action="cgi-bin/login.cgi" method="get"> <input type="text" name="theText"> <input type="submit" value="Continue"> </form> </body> </html>
新建文件夹,放入新建的login.c,再拷贝cgic.h cgic.h在当前文件夹下,
login.c内容:
#include <stdio.h> #include "cgic.h" #include <string.h> #include <stdlib.h> extern char *cgiQueryString; int cgiMain() { cgiHeaderContentType("text/html"); fprintf(cgiOut, "<HTML><HEAD>/n"); fprintf(cgiOut, "<TITLE>My CGIC</TITLE></HEAD>/n"); fprintf(cgiOut, "<BODY>"); fprintf(cgiOut, "<H1>%s</H1>",cgiQueryString); fprintf(cgiOut, "</BODY>/n"); fprintf(cgiOut, "</HTML>/n"); return 0; }
新建Makefile文件:
login.cgi:cgic.h cgic.c gcc login.c cgic.c -o login.cgi clean: rm -f *.o *.a *.cgi
执行 make,将生成的login.cgi放入cgi-bin目录中,运行启动boa服务器:./boa
浏览器中输入: http://192.168.1.103/login.html
得到界面
点击Continue后得到
get请求会在URL中附带明文,这样缺少保密性,故用post请求。
POST请求demo:
login.html
<html> <head> <title>Test</title> </head> <body> <form action="cgi-bin/login.cgi" method="POST"> <input type="text" name="name" /> <input type="text" name="number" /> <input type="submit" value="Submit" /> </form> </body> </html>
login.c
#include <stdio.h> #include "cgic.h" #include <string.h> #include <stdlib.h> int cgiMain() { char name[241]; char number[241]; cgiHeaderContentType("text/html"); fprintf(cgiOut, "<HTML><HEAD>\n"); fprintf(cgiOut, "<TITLE>My CGI</TITLE></HEAD>\n"); fprintf(cgiOut, "<BODY>"); cgiFormString("name", name, 241); cgiFormString("number", number, 241); fprintf(cgiOut, "<H1>%s</H1>",name); fprintf(cgiOut, "<H1>%s</H1>",number); fprintf(cgiOut, "</BODY>\n"); fprintf(cgiOut, "</HTML>\n"); return 0; }
make 后,拷贝生成的login.cgi到cgi-bin目录下, 浏览器输入可查看效果:http://192.168.1.103/login.html
点击提交后,效果为:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步