How to use "TcppWebBrowser" in C Builder POST data
Mountain note: I will introduce the CPPWebBrowser. In fact, Borland's component is a package in the form of a VCL component of the Microsoft Internet Control ActiveX control. If you want to get more powerful features, huh, you can't say it, you have to study the "IHTMLDocument2" interface pointer. This thing has no detailed description in BCB and Delphi's help, can only check MSDN. Of course, this article does not involve this trouble.
Summary: This article demonstrates how to browse and post data using the "NaviGate" and "NaviGate2" methods of "CPPWebBrowser" components. The author is adam vieweger.
These two weeks have bare my hair almost torn! I want to use the "TCPWebBrowser" component (first included in the Borland C Builder 5 Enterprise Edition) Write a program that can browse the web and allow the user to send POST data to the web page. This is really depressed for a while, because "TcppWebBrowser" has very few documents, and there is not much article for me to boot. Ok, then I will roll up the sleeves and start working! I hope that my discovery is useful for you. It is very simple to implement browsing features with "CPPWebBrowser". There are two ways to provide browsing features: "NaviGate" and "NaviGate2":
CPPWebBrowser1-> navigate ("http://www.inprise.com")
"NaviGate2" is an extension to "NaviGate", but in fact, this doesn't matter to us - in this article, we can use any of these two. If you want to learn more about the properties, methods, and events of the WebBrowser component, see msdn: http://msdn.microsoft.com/library/default.asp? Url = / workshop / Browser / WebBrowser / Reference / Objects / Webbrowser.asp
Puzzle POSTING DATA is very difficult to crack. The components of Microsof require data to pass through a "SafeArray" structure. The problem is that all "navigate2" parameters are "TVARIANT" type. I have to convert between "SafeArray" and "Tvariant".
In Borland Community (Note: This is the Borland's Developer Community), I saw a tutorial of a giant, how to use SafeArrays in C Builder. If you want more detailed information, see: SafeArrays Made Easier (http://community.borland.com/Article/0, 1410, 22016,00.html) The following code demonstrates the results of my research. "Navigate" requires its TVARIANT is a VB_Array type and pointing to a one-dimensional array of VT_UT1 (an unsigned integer) type element, and it should contain a element to record the word to send data. Section number. fainted? Ok, let's take a look at the code, all the concept will be clearer: // * method 1 * tvariant vTempty; tvariant vtpostdataArray; char * str = "action = logmein & username = myname & password = mypass"; SafeArray Far * PSA = NULL SafeArrayBound Sabound [48]; Sabound [0] .CELEMENTS = Strlen (Str); Sabound [0] .llbound = 0; PSA = SafeArrayCreate (VT_UI1, 1, Sabound); for (unsigned int N = 0; n TVariant vAddress = { "http: //my.server/test/postresults.asp"}; CppWebBrowser1-> Navigate2 (& vAddress, & vtEmpty, & vtEmpty, & vtPostDataArray, & vtEmpty); SafeArrayDestroy (psa); // * Method 2 * TVariant vtEmpty; char * str = "action = LogMeIn & username = MyName & password = MyPass"; TSafeArrayDim1 dim (strlen (str)); TSafeArrayUInt1 uint_array (dim); for (unsigned int n = 0; n vtEmpty.vt = VT_EMPTY; TVariant vAddress = { "http: //my.server/test/postresults.asp"}; CppWebBrowser1-> Navigate2 (& vAddress, & vtEmpty, & vtEmpty, & sa, & vtEmpty); SafeArrayDestroy (sa); Method 1 And Method 2 is a very perfect code, but they all have only one shortcoming - they can't work! All correctly passed the data, but "NaviGate" does not give the data POST to the specified URL. Deep mining me back to MSDN and then study WebBrowser. At this time, I found two articles that were very helpful to me: Q167658 and Q165800. I have those information on the hand, I finally succeeded! This is an improvement to my code. This The code demonstrates how to browse and post "http" data using a CPPWebBrowser component. You can further modify these code to accommodate your requirements. Void WebPostData (TCPPWebBrowser * CPPWebBBrowser, String Surl, String Spostdata) {BSTR bstrHeaders = NULL; TVariant vFlags = {0}, vTargetFrameName = {0}, vPostData = {0}, vHeaders = {0}; LPSAFEARRAY psa; LPCTSTR cszPostData = sPostData.c_str (); UINT cElems = lstrlen (cszPostData) LPSTR PPSTDATA; LPVARIANT PVPOSTDATA; bstrHeaders = SysAllocString (L "Content-Type: application / x-www-form-urlencodedrn"); if (bstrHeaders!) {Application-> MessageBox ( "Could not allocate bstrHeaders", "Warning", MB_OK | MB_ICONWARNING); return } V_vt (& vheaders) = vt_bstr; v_bstr (& vheaders) = BSTRHEADERS; PvpostData = vpostdata; IF (pvpostdata) {variantinit (pvpostdata); PSA = SafeArrayCreateVector (VT_UI1, 0, CELEMS); if (! psa) {return;} SafearrayAccessData (PSA, (LPVOID *) & PPostData); Memcpy (PPostData, CSZPostData, CelearrayunAccessData (PSA); V_vt (pvpostdata) = vt_Array | vt_ui1; v_array (pvpostdata) = psa;} CPPWebBrowser-> NaviGate ((Tvariant) SURL, & VFLAGS, & VTARGETFRAMENAME, & VPOSTDATA, & VHEADERS) I have no reason to believe that this code is perfect. But for my previous efforts, it only has one advantage: it can work! I hope to help you with WebBrowser in C Builder. Translation: I can see the mountain today