Put a part of the code first
CString strhttpname="http://localhost/TestReg/RegForm.aspx";//pages that need to submit dataCString Strformdata ="username=abc&password=123";//data that needs to be submittedCInternetSession Sess;
CHttpFile*FileGet;
CString strheaders= _t ("content-type:application/x-www-form-urlencoded");//Request Header Try{FileGet= (chttpfile*) Sess. OpenURL (Strhttpname);//Open File } Catch(cexception*e) {FileGet=0; Throw; } CString strsentence, Strgetsentence=""; 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>= $&&dwStatus< -) {
BOOL result = fileget->sendrequest (strheaders, (LPVOID) (LPCTSTR) Strformdata, Strformdata.getlength ());
while(Fileget->readstring (strsentence))//return results After reading the submitted data{strgetsentence= strgetsentence + strsentence +Char( -) +Char(Ten); } AfxMessageBox (Strgetsentence); //Show back Page content } Else{Strsentence.format ("post error, error code:%d", dwstatus); AfxMessageBox (strsentence); } FileGet-Close (); Delete FileGet; } ElseAfxMessageBox ("cannot find web file! "); Sess. Close ();
Because of the need to use post to crawl the content returned by a webpage, so found in MFC features are not so powerful chttpfile to do, also enough.
General network programming of the demo are ANSI character sets, also have no problem, the above code to change can use, get the Unicode project, will add _t identifier and do some character set conversion, soon can run, but the problem came, is sent to the request always can not make the server correctly accept, and return some errors, this debugging does not matter, 2 hours passed.
Finally, in the case of PHP program and MFC program, the problem is locked in the underlined code, the ANSI is sent Char characters, is not a problem, and in the Unicode case, the past is WCHAR characters, and compile without error, even in the Unicode version, This function is also expected to accept Char characters.
Well, the library function is written like this, it's gem enough.
An error using the CHttpFile