REQUEST_METHOD的值一般包括POST和GET两种
GET参数的获取
在GET方法下,CGI程序无法直接从服务器的标准输入中获取数据,因为服务器把从标准输入接收到的数据编码到环境变量QUERY_STRING(或PATH_INFO),采用GET方法时,只需把这些数据附加到URL的末尾,如http://192.168.1.211:8080/cgi-bin/222?s=1&b=3&u=0,这时候QUERY_STRING的值为s=1&b=3&u=0
#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
std::vector<std::string> StringSplit(const std::string &sData,const std::string &sDelim)
{
std::vector<std::string> vItems;
vItems.clear();
std::string::size_type bpos = 0;
std::string::size_type epos = 0;
std::string::size_type nlen = sDelim.size();
while((epos=sData.find(sDelim,epos)) != std::string::npos)
{
vItems.push_back(sData.substr(bpos,epos-bpos));
epos +=nlen;
bpos = epos;
}
vItems.push_back(sData.substr(bpos,sData.size()-bpos));
return vItems;
}
int get_method()
{
std::cout<<"Content-type:text/html\r\n\r\n";
std::cout<<"<html>\n";
std::cout<<"<head>\n";
std::cout<<"<title> Hello World -First CGI Program</title>\n";
std::cout<<"</head>\n";
std::cout<<"<body>\n";
std::cout<<"get parameter:<br/>";
char *value = getenv("QUERY_STRING");
if(value != 0)
{
std::vector<std::string>paras = StringSplit(std::string(value),std::string("&"));
std::vector<std::string>::iterator iter = paras.begin();
for( ;iter != paras.end(); iter++)
{
std::vector<std::string>singlepara = StringSplit(*iter,std::string("="));
std::cout<<singlepara[0]<<" "<<singlepara[1]<<"<br/>";
}
}
std::cout<<"</body>\n";
std::cout<<"</html>\n";
return 0;
}
int main()
{
get_method();
return 0;
}
g++ -g 2.cpp -o 222
root@ubuntu:/share/http/apache2.2.32-install# cp /share/222 cgi-bin/
![](https://img-blog.csdn.net/20170817213602858)