我继承了一个使用CppWebBrowser控件的旧应用程序 应用程序一直使用:: Navigate方法进行GET请求,但现在我需要开始将它用于POST;但是,现有代码不适用于POST。我使用这些数据类型的知识有限但我怀疑它与CString到VT_ARRAY | VT_UI1转换有关。
void WebPostData(TCppWebBrowser *CppWebBrowser, CString sURL, CString sPostData)
{
BSTR bstrHeaders = NULL;
TVariant vFlags = { 0 }, vTargetFrameName = { 0 }, vPostData = { 0 }, vHeaders = { 0 };
LPSAFEARRAY psa;
LPCTSTR cszPostData = sPostData;
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);
}
答案 0 :(得分:1)
您展示的代码是从此C ++ Builder示例中复制的:
How to post data using TCppWebBrowser in C++Builder
CString
是一个MFC类,但C ++ Builder不支持MFC。原始代码使用System::String
代替,这是一个C ++ Builder类。那么,您是否尝试将此代码转换为Visual Studio?或者你真的在使用C ++ Builder吗?你没说。
我将假设您正在使用C ++ Builder,否则您必须重新编写Visual Studio类的代码。
因此,让我们首先将CString
更改回String
。现在,根据您使用的C ++ Builder版本,String
是 ANSI (C ++ Builder 3-2007)或 UTF-16 (C ++ Builder 2009+)。在后一种情况下,有一个_DELPHI_STRING_UNICODE
预编译器定义。
application/x-www-form-urlencoded
类型通常不以UTF-16格式发送,并且无法保证Web服务器能够处理它。所以我建议你将sPostData
数据转换为UTF-8,然后你可以创建一个UTF-8字节的VT_UI1数组:
#ifndef _DELPHI_STRING_UNICODE
inline bool NeedsUTF8(String s)
{
// String is 1-indexed
for (int i = 1; i <= s.Length(); ++i)
{
int ch = s[i];
if (ch > 0x7F) return true;
}
return false;
}
#endif
void WebPostData(TCppWebBrowser *CppWebBrowser, String sURL, String sPostData)
{
TVariant vFlags, vTargetFrameName, vPostData, vHeaders;
// note: UTF8String was added in C++Builder 6,
// use AnsiString in C++Builder 3-5 instead
UTF8String utf8;
#ifdef _DELPHI_STRING_UNICODE
// C++Builder 2009+, convert from UTF-16 to UTF-8 directly
utf8 = sPostData;
#else
// C++Builder 3-2007, check if ASCII or ANSI
if (NeedsUTF8(sPostData))
{
// ANSI, so convert from ANSI to UTF-16 to UTF-8,
// but you have to do the last step manually
WideString wPostData(sPostData);
int len = WideCharToMultiByte(CP_UTF8, wPostData.c_bstr(), wPostData.Length(), NULL, 0, NULL, NULL);
utf8.SetLength(len);
WideCharToMultiByte(CP_UTF8, wPostData.c_bstr(), wPostData.Length(), utf8.c_str(), len, NULL, NULL);
}
else
{
// ASCII, compatible with UTF-8 as-is
utf8 = sPostData;
}
#endif
LPSAFEARRAY psa = SafeArrayCreateVector(VT_UI1, 0, utf8.Length());
if (!psa)
return;
// TVariant takes ownership of a safearray
vPostData = psa;
// TVariant takes ownership of a copy of a WideString's BSTR
vHeaders = WideString("Content-Type: application/x-www-form-urlencoded;charset=utf-8\r\n");
LPVOID pPostData;
if (FAILED(SafeArrayAccessData(psa, &pPostData)))
return;
memcpy(pPostData, utf8.c_str(), utf8.Length());
SafeArrayUnaccessData(psa);
CppWebBrowser->Navigate((TVariant)sURL, &vFlags, &vTargetFrameName, &vPostData, &vHeaders);
}