CPPWebBrowser的PostData[VC/C++编程]
本文“CPPWebBrowser的PostData[VC/C++编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
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);
这样是不行的,网页收不到参数.
http://www.bianceng.cn/maker/pass.asp?username=test&password=12345
这样是可以拜候的.
请示该若何设置PostData的值?
给你一个直接Post的函数
如:
WebPostData(CppWebBrowser1,"http://211.91.2.221/pass.asp";
,"test=333333&PassWord=343200");
我试过,可以用
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 data using TCppWebBrowser in C++Builder (转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 my hair out for more than two weeks. I wanted to use the TCppWebBrowser component (distributed with Borland C++Builder starting with BCB 5 Enterprise Edition) to create an app that would browse the Web and allow users to post data to Web pages. I was having a tough time of it because the documentation on TCppWebBrowser is fairly sparse and there aren't many articles out there to guide me.
So I rolled up my sleeves and started working. I hope my discoveries prove useful to you.
Browsing with CppWebBrowser turned out to be 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 for us -- in this article we can use both of them interchangeably.
Should you want to find more information about WebBrowser component properties, methods, and events, please refer to online MSDN library: http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/webbrowser/reference/Objects/WebBrowser.asp
HARDER PROBLEMS
Posting data proved to be a tougher nut to crack. Microsoft's component requires data to be passed in a SAFEARRAY structure. The problem is that all 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 use SAFEARRAYs with C++Builder. For further details please refer to SAFEARRAYs made easier.
The following code demonstrates the results of my research. Navigate requires that the TVariant should be of type VB_ARRAY and that it should point to a SAFEARRAY. The SAFEARRAY should be of element type VT_UI1 (an 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 be 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);
以上是“CPPWebBrowser的PostData[VC/C++编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |