Cppwebbrowser's PostData

Source: Internet
Author: User
Tags strlen

Widestring URL = "http://www.bianceng.cn/maker/pass.asp";;

paramstring = "username=test&password=12345";

Tvariant *postdata = new Tvariant (paramstring);

Cppwebbrowser->navigate (url,0,0,postdata,0);

This is not possible, the Web page does not receive parameters.

http://www.bianceng.cn/maker/pass.asp?username=test&password=12345

This is accessible.

How do I set the value of PostData?

I'm giving you a direct post function.

Such as:

Webpostdata (CppWebBrowser1, "http://211.91.2.221/pass.asp";

, "test=333333&password=343200");

I've tried, I can use it.

void __fastcall Tform1::webpostdata (tcppwebbrowser *cppwebbrowser, 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 Ppostdata;
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, Celems);
Safearrayunaccessdata (PSA);
V_VT (pvpostdata) = Vt_array | VT_UI1;
V_array (pvpostdata) = PSA;
}
Cppwebbrowser->navigate ((tvariant) sURL, &vflags, &vtargetframename, &vpostdata, &vHeaders);
}
How to post the data using Tcppwebbrowser in C++builder (Turn www.borland.com)
Abstract:this article demonstrates techniques for using the Cppwebbrowser component to browse and post data using Navigate and Navigate2 methods. by Adam Vieweger.
I had been tearing I hair out for more than two. I wanted to use the Tcppwebbrowser component (distributed with Borland C++builder starting with BCB 5 Enterprise Edition) To create a app that would browse the Web and allow users to post data to Web pages. I was has a tough time of it because the documentation on Tcppwebbrowser is fairly sparse and there aren ' t many article s out there to guide me.
So I rolled up my sleeves and started working. I hope my discoveries prove useful.
The browsing with Cppwebbrowser turned out to is fairly simple. There are two methods that provide browsing capabilities:navigate and Navigate2:
Cppwebbrowser1->navigate ("http://www.inprise.com";)
Navigate2 is extension of Navigate, but practically it does not matter to us-in this article we can use both of them I nterchangeably.
Should you want the information about WebBrowser component properties, methods, and events, please refer to Onlin E MSDN library:http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/webbrowser/reference/objects/ Webbrowser.asp
Harder PROBLEMS
Posting data proved to being a tougher nut to crack. Microsoft ' s component requires data to is passed in a SAFEARRAY structure. The problem is Navigate2 parameters are tvariant type. I needed to convert between SAFEARRAY and Tvariant.
That's where the Borland Community site came in handy. I found a great tutorial on "How to" SAFEARRAYs with C++builder. For further details, refer to SAFEARRAYs made easier.
The following code demonstrates the results of my. Navigate requires that the tvariant should is of type vb_array and that it should point to a SAFEARRAY. The SAFEARRAY should be of element type VT_UI1 (a unsigned int), dimension one, and it should have an element count equal To the number of bytes of data to be posted.
Confused? Take a look at the code and everything should is much 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 < strlen (str); n++) {
SafeArrayPutElement (PSA, (long*) 0, (void*) str[n]);
}
VTEMPTY.VT = Vt_empty;
VTPOSTDATAARRAY.VT = Vt_array;
Vtpostdataarray.setsafearray (PSA);
or VTPOSTDATAARRAY=PSA;
Tvariant vaddress = {"http://my.server/test/postresults.asp";};
Cppwebbrowser1->navigate2 (&vaddress, &vtempty, &vtempty, &vtpostdataarray, &vtEmpty);
SafeArrayDestroy (PSA);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.