cout << "Content-Type: text/html\n\n";
cout << "<html>\n";
cout << " <head>\n";
...
cout << " </body>\n";
cout << "</html>\n";
Here are some other content types:
| text/plain | TXT files |
| application/vnd.ms-excel | tab-delimited CSV files |
| vnd.wap.wml | WML wireless web pages |
Rename the EXE to CGI, and place it in the /cgi-bin (or /scripts) folder on a web server.
int main()
{
// to process a GET
string s = getenv("QUERY_STRING");
...
// to process a POST
int contentLength = 0;
char postString[1024] = {0};
string s = "";
if (getenv("CONTENT_LENGTH"))
contentLength = atoi(getenv("CONTENT_LENGTH"));
if (contentLength)
cin.read(postString, contentLength);
s = postString;
...
return 0;
}
See: details about getenv
#include <cstdlib> // OPTIONAL: for getenv
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char* buf = "Hello, World";
// this is what makes it work:
cout << "Content-Type: text/html\n\n";
// just some content...
cout << "<html><head><title></title></head>"
<< "<body>\n";
cout << "The string length of\n\"" << buf
<< "\"\nis " << strlen(buf)
<< " characters.\n";
cout << "<p>\n";
// OPTIONAL
// if there is a query string
// (appended to the URL by a "?")...
char* queryString = getenv("QUERY_STRING");
if (queryString)
cout << "The Query String is: <pre>"
<< queryString << "</pre>\n\n";
// OPTIONAL
// read the complete client request
// (including cookies, environment vars,...)
int contentLength = 0;
char postString[1024] = {0};
if (getenv("CONTENT_LENGTH"))
contentLength = atoi(getenv("CONTENT_LENGTH"));
if (contentLength)
cin.read(postString, contentLength);
cout << "<pre>" << postString << "</pre>";
// ...and the closing tags
cout << "</body></html>\n";
cout.flush(); // do not forget this!
return 0;
}