目录
cgi程序
#include<stdio.h>
#include<stdlib.h>
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
char username[256];
char password[256];
char src[256];
printf("Content-Type:text/html;charset=utf-8\r\n\r\n");
printf(getenv("REQUEST_METHOD"));
//获取请求的方式
if(!strcmp(getenv("REQUEST_METHOD"),"GET"))
{
//直接通过环境变量得到get的请求参数
char * qs=getenv("QUERY_STRING"); //获取get数据
sscanf(qs,"username=%[^&]&passwd=%s",username,password);
printf("<html><head></head><body>username:%s<br/>passwd:%s <font color='red'>hello world</font></body></html>",username,password);
}
// printf(getenv("REMOTE_HOST"));
// printf(getenv("GATEWAY_INTERFACE")); //cgi协议的版本
if(!strcmp(getenv("REQUEST_METHOD"),"POST"))
{
/*获取post数据*/
//post传送的数据在数据流中
cin>>src;
sscanf(src,"username=%[^&]&passwd=%s",username,password);
printf("<html><head></head><body>username:%s<br/>passwd:%s <font color='red'>hello world</font></body></html>",username,password);
}
return 0;
}
cgi是沟通HTML表单和服务器端程序的接口
上面的代码显示了如何获取到客户端发来的get或者post请求的参数
HTML简单界面
<!DOCTYPE html>
<html>
<head>
<title>post get data
</title>
<meta charset="utf-8">
</head>
<body>
<form name="form1" action="web3.cgi" method="post">
<input type="text" name="username">
<input type="password" name="passwd">
<input type="submit" value="submit">
</form>
</body>
</html>
1.界面如下:form表单使用post方法
URL不携带请求参数,获得参数结果如下:
2.使用GET请求参数