Read Web pages, and GET method
Next page: POST data as a form.
There's 2 ways to send data to web. One is GET, another is POST
You can read RFC for difference between GET and POST, but briefly, when you
read ¡°http://server/cgi.dll?a=1&b=2&c=3&d=4¡±, you are sending
data "a=1, b=2, c=3, d=4" to "http://server/cgi.dll" this CGI program, using
GET method.
In MFC, CHttpFile is a subclass of CStdioFile. After you open a file in Internet using
CHttpFile, you can ReadString like CStdioFile. You must use CInternetSession to open
internet file.
Here is a part of source code of Chinese URL System,
this function find new template and input it into Chinese URL System. Application
Get handle from sess.OpenURL(strFileName) and then
fileGet->QueryInfo to check status of this file.
If success, the return value should be 200-299. After that, we can read that web file
like usual:
while(fileGet->ReadString(strSentence))
fileWrite.WriteString(strSentence+"\n");
fileWrite.Close();
Now, you know how to read page, you then you can send data using GET method.
For example, when you read "http://www.fadshop.net/curl/progsave.asp?curl=&http=http://www.china.com",
you are submitting your website to server of Chinese URL System.
But, you can not sent too many data using GET method. Then you should use POST.
you can read next page: POST data
Source code below is verified using NT4, Visual C6.0
CString strWriteName="Template01.htm"
#ifndef _DEBUG
strFileName="http://www.fadshop.net/curl/" + strWriteName;
#else //In DEBUG version, read from my IIS.
strFileName="http://127.0.0.1/fadshop/curl/" + strWriteName;
#endif
CInternetSession sess;//Create session
CHttpFile* fileGet;
CException* e;
TRY
{
fileGet=(CHttpFile*)sess.OpenURL(strFileName);//Open file
}
CATCH_ALL(e)
{
fileGet = 0;
}
END_CATCH_ALL
if(fileGet)
{
DWORD dwStatus;
DWORD dwBuffLen = sizeof(dwStatus);
BOOL bSuccess = fileGet->QueryInfo(
HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER,
&dwStatus, &dwBuffLen);
if( bSuccess && dwStatus>= 200&& dwStatus<300 )
{ CStdioFile fileWrite;
if(fileWrite.Open(strWriteName, CFile::modeWrite|CFile::modeCreate))
{
while(fileGet->ReadString(strSentence)){
fileWrite.WriteString(strSentence+"\n");
}
fileWrite.Close();
SetDlgItemText(IDC_DOWNMSG, "Downloaded, installing...");
How to install, it's none of our business
}// if openfile success.
else SetDlgItemText(IDC_DOWNMSG,
"Local file"+strWriteName+"Error\n");
}// if bSuccess.
else SetDlgItemText(IDC_DOWNMSG, "No new template.\n
Please run it later, or visit my homepage for further message");
fileGet->Close();
delete fileGet;
}
else
SetDlgItemText(IDC_DOWNMSG, "Network Problem");
sess.Close();
|