Using CHttpFile to realize simple get/post data "turn"
Last Update:2016-06-28
Source: Internet
Author: User
<span id="Label3"></p><p><p>one, get data, download Web pages, files, etc., for downloadable files, not for the server running programs, such as. aspx files, or else will return a 500 error.</p></p><p><span style="color: #000000;">CString strsentence, Strwritename<span style="color: #000000;">=<span style="color: #000000;">"<span style="color: #000000;">1.htm<span style="color: #000000;">"<span style="color: #000000;">;<br>CString strFileName<span style="color: #000000;">=<span style="color: #000000;">"<span style="color: #000000;">http://localhost/InDesign/<span style="color: #000000;">"<span style="color: #000000;"><span style="color: #000000;">+<span style="color: #000000;">strwritename;<br><br>CInternetSession sess;<br>CHttpFile<span style="color: #000000;">*<span style="color: #000000;">fileGet;<br><span style="color: #0000ff;">Try<span style="color: #000000;"><br><span id="Codehighlighter1_161_213_Open_Text"><span style="color: #000000;">{<br>FileGet<span style="color: #000000;">=<span style="color: #000000;">(chttpfile<span style="color: #000000;">*<span style="color: #000000;">) Sess. OpenURL (strfilename);<br>}<span style="color: #000000;"><br><span style="color: #0000ff;">Catch<span style="color: #000000;">(cexception<span style="color: #000000;">*<span style="color: #000000;">E<br><span id="Codehighlighter1_238_265_Open_Text"><span style="color: #000000;">{<br>FileGet<span style="color: #000000;">=<span style="color: #000000;"><span style="color: #000000;">0<span style="color: #000000;">;<br><span style="color: #0000ff;">Throw<span style="color: #000000;">;<br>}<span style="color: #000000;"><br><br><span style="color: #0000ff;">If<span style="color: #000000;">(fileGet)<br><span id="Codehighlighter1_283_977_Open_Text"><span style="color: #000000;">{<br>DWORD dwstatus;<br>DWORD Dwbufflen<span style="color: #000000;">=<span style="color: #000000;"><span style="color: #0000ff;">sizeof<span style="color: #000000;">(dwstatus);<br>BOOL bsuccess<span style="color: #000000;">=<span style="color: #000000;">FileGet<span style="color: #000000;">-<span style="color: #000000;">QueryInfo (http_query_status_code<span style="color: #000000;">|<span style="color: #000000;">http_query_flag_number,<span style="color: #000000;">&<span style="color: #000000;">dwstatus,<span style="color: #000000;">&<span style="color: #000000;">dwbufflen);<br><br><span style="color: #0000ff;">If<span style="color: #000000;">(bsuccess<span style="color: #000000;">&&<span style="color: #000000;">Dwstatus<span style="color: #000000;">>=<span style="color: #000000;"><span style="color: #000000;">200<span style="color: #000000;">&&<span style="color: #000000;">Dwstatus<span style="color: #000000;"><<span style="color: #000000;">300<span style="color: #000000;">)<br><span id="Codehighlighter1_503_837_Open_Text"><span style="color: #000000;">{<br>CStdioFile fileWrite;<br><span style="color: #0000ff;">If<span style="color: #000000;">(filewrite.open (strwritename, Cfile::modewrite<span style="color: #000000;">|<span style="color: #000000;">Cfile::modecreate))<br><span id="Codehighlighter1_607_766_Open_Text"><span style="color: #000000;">{<br><span style="color: #0000ff;">While<span style="color: #000000;">(fileGet<span style="color: #000000;">-<span style="color: #000000;">ReadString (strsentence))<br><span id="Codehighlighter1_658_711_Open_Text"><span style="color: #000000;">{<br>Filewrite.writestring (strsentence<span style="color: #000000;">+<span style="color: #000000;">"<span style="color: #000000;">\ n<span style="color: #000000;">"<span style="color: #000000;">);<br>}<span style="color: #000000;"><br>Filewrite.close ();<br>AfxMessageBox (<span style="color: #000000;">"<span style="color: #000000;">Download complete<span style="color: #000000;">"<span style="color: #000000;">);<br>}<span style="color: #000000;"><br><span style="color: #0000ff;">Else<span style="color: #000000;"><br> <span id="Codehighlighter1_779_833_Open_Text"><span style="color: #000000;">{<br>AfxMessageBox (<span style="color: #000000;">"<span style="color: #000000;">Local file<span style="color: #000000;">"<span style="color: #000000;">+<span style="color: #000000;">Strwritename<span style="color: #000000;">+<span style="color: #000000;">"<span style="color: #000000;">Error Opening.<span style="color: #000000;">"<span style="color: #000000;">);<br>}<span style="color: #000000;"><br>}<span style="color: #000000;"><br><span style="color: #0000ff;">Else<span style="color: #000000;"><br><span id="Codehighlighter1_849_936_Open_Text"><span style="color: #000000;">{<br>Strsentence.format (<span style="color: #000000;">"<span style="color: #000000;"><span style="color: #000000;"> error opening Web page file, error code:%d<span style="color: #000000;"> <span style="color: #000000;" dwstatus); <br> afxmessagebox (strSentence); <br> } <span style="color: #000000;"> <br> fileget <span style="color: #000000;">-><span style="color: #000000;">close (); <br> delete fileGet; <br> } <span style="color: #000000;"> <br> <span style="color: #0000ff;">else<span style="color: #000000; "> <br> afxmessagebox (<span style="color: #000000;"> "<span style=" Color: #000000; "> can't find web files! <span style="color: #000000;"> "<span style=" color: #000000; ">); <br> <br> sess. Close (); </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p><p><p><span style="color: #000000;">second, POST data, such as for the submission of registration information, etc.</span></p></p><p><span style="color: #000000;"><span style="color: #000000;">CString Strhttpname<span style="color: #000000;">=<span style="color: #000000;">"<span style="color: #000000;">Http://localhost/TestReg/RegForm.aspx<span style="color: #000000;">"<span style="color: #000000;">;<span style="color: #008000;">//<span style="color: #008000;">Pages that need to submit data<span style="color: #008000;"><br><span style="color: #000000;">CString Strformdata<span style="color: #000000;">=<span style="color: #000000;"><span style="color: #000000;">"<span style="color: #000000;">Username=abc&password=123<span style="color: #000000;">"<span style="color: #000000;">;<span style="color: #008000;">//<span style="color: #008000;">Data that needs to be submitted<span style="color: #008000;"><br><span style="color: #000000;"><br>CInternetSession sess;<br>CHttpFile<span style="color: #000000;">*<span style="color: #000000;">fileGet;<br>CString strheaders<span style="color: #000000;">=<span style="color: #000000;">_t (<span style="color: #000000;">"<span style="color: #000000;">content-type:application/x-www-form-urlencoded<span style="color: #000000;">"<span style="color: #000000;">);<span style="color: #008000;">//<span style="color: #008000;">Request Header<span style="color: #008000;"><br><span style="color: #000000;"><br><span style="color: #0000ff;">Try<span style="color: #000000;"><br><span id="Codehighlighter1_274_332_Open_Text"><span style="color: #000000;">{<br>FileGet<span style="color: #000000;">=<span style="color: #000000;">(chttpfile<span style="color: #000000;">*<span style="color: #000000;">) Sess. OpenURL (strhttpname);<span style="color: #008000;">//<span style="color: #008000;">Open File<span style="color: #008000;"><br><span style="color: #000000;">}<span style="color: #000000;"><br><span style="color: #0000ff;">Catch<span style="color: #000000;">(cexception<span style="color: #000000;">*<span style="color: #000000;">E<br><span id="Codehighlighter1_357_384_Open_Text"><span style="color: #000000;">{<br>FileGet<span style="color: #000000;">=<span style="color: #000000;"><span style="color: #000000;">0<span style="color: #000000;">;<br><span style="color: #0000ff;">Throw<span style="color: #000000;">;<br>}<span style="color: #000000;"><br><br>CString strsentence, strgetsentence<span style="color: #000000;">=<span style="color: #000000;"><span style="color: #000000;">""<span style="color: #000000;">;<br><span style="color: #0000ff;">If<span style="color: #000000;">(fileGet)<br><span id="Codehighlighter1_444_1102_Open_Text"><span style="color: #000000;">{<br>DWORD dwstatus;<br>DWORD Dwbufflen<span style="color: #000000;">=<span style="color: #000000;"><span style="color: #0000ff;">sizeof<span style="color: #000000;">(dwstatus);<br>BOOL bsuccess<span style="color: #000000;">=<span style="color: #000000;">FileGet<span style="color: #000000;">-<span style="color: #000000;">QueryInfo (http_query_status_code<span style="color: #000000;">|<span style="color: #000000;">http_query_flag_number,<span style="color: #000000;">&<span style="color: #000000;">dwstatus,<span style="color: #000000;">&<span style="color: #000000;">dwbufflen);<br><span style="color: #0000ff;">If<span style="color: #000000;">(bsuccess<span style="color: #000000;">&&<span style="color: #000000;">Dwstatus<span style="color: #000000;">>=<span style="color: #000000;"><span style="color: #000000;">200<span style="color: #000000;"><span style="color: #000000;">&&<span style="color: #000000;">Dwstatus<span style="color: #000000;"><<span style="color: #000000;">300<span style="color: #000000;">)<br><span id="Codehighlighter1_662_961_Open_Text"><span style="color: #000000;">{<br>BOOL result<span style="color: #000000;">=<span style="color: #000000;">FileGet<span style="color: #000000;">-<span style="color: #000000;">SendRequest (strheaders, (lpvoid) (lpctstr) strformdata, strformdata.getlength ());<br><span style="color: #0000ff;">While<span style="color: #000000;">(fileGet<span style="color: #000000;">-<span style="color: #000000;">ReadString (strsentence))<span style="color: #008000;">//<span style="color: #008000;">Return results after reading the submitted data<span style="color: #008000;"><br><span style="color: #000000;"><span id="Codehighlighter1_833_911_Open_Text"><span style="color: #000000;">{<br>Strgetsentence<span style="color: #000000;">=<span style="color: #000000;">Strgetsentence<span style="color: #000000;">+<span style="color: #000000;">Strsentence<span style="color: #000000;">+<span style="color: #000000;"><span style="color: #0000ff;">Char<span style="color: #000000;">(<span style="color: #000000;">13<span style="color: #000000;">)<span style="color: #000000;">+<span style="color: #000000;"><span style="color: #0000ff;">Char<span style="color: #000000;">(<span style="color: #000000;">10<span style="color: #000000;">);<br>}<span style="color: #000000;"><br>AfxMessageBox (strgetsentence);<span style="color: #008000;">//<span style="color: #008000;">Show back Page content<span style="color: #008000;"><br><span style="color: #000000;">}<span style="color: #000000;"><br> <span style="color: #0000ff;">Else<span style="color: #000000;"><br><span id="Codehighlighter1_973_1058_Open_Text"><span style="color: #000000;">{<br>Strsentence.format (<span style="color: #000000;">"<span style="color: #000000;">Post error, error code:%d<span style="color: #000000;"><span style="color: #000000;"> "<span style=" color: #000000; ">, dwstatus); <br> afxmessagebox (strSentence); <br> } <span style="color: #000000;"> <br> <br> fileget<span style="color: #000000;">-><span style="color: #000000;">close (); <br> delete fileGet; <br> } <span style="color: #000000;"> <br> <span style="color: #0000ff;">else<span style="color: #000000; "> <br> afxmessagebox (<span style="color: #000000;"> "<span style=" Color: #000000; "> can't find web files! <span style="color: #000000;"> "<span style=" color: #000000; ">); <br> <br> sess. Close (); </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p><p><p> Use CHttpFile to implement simple Get/post data "go" </p> </p></span>