Iwebbrowser2导航:没有提交后期数据

时间:2015-01-05 08:33:13

标签: c++ post com iwebbrowser2

我正在使用IWebbrowser2界面来调用页面并向其提交一些POST数据。 IE被打开,页面被调用,但遗憾的是没有提交POST数据(PHP-Server上的$ _POST-Array为空)。有人可以告诉我为什么吗?

主要方法的相关部分:

BSTR bstrURL2;
bstrURL2 = SysAllocString(L"http://www.mypage.com/");

// Specify a binary Content-Type.
V_BSTR(&vHeaders) = SysAllocString(
  L"Content-Type: application/x-www-form-urlencoded\r\n" 
  L"Content-Encoding: UTF-8\r\n");

VARIANT vPostData = {0};
if (Navigate::GetPostData(&vPostData) == NOERROR)
r->Navigate(bstrURL2, &vFlags, &vEmpty, &vPostData, &vHeaders);

GetPostData-method:

HRESULT Navigate::GetPostData(LPVARIANT pvPostData)
{
  HRESULT hr;
  LPSAFEARRAY psa;
  LPCTSTR cszPostData = "username=myusername&password=mypassword";
  UINT cElems;
  LPSTR pPostData;

  cElems = lstrlen(cszPostData);

  if (!pvPostData)
  {
    return E_POINTER;
  }

  VariantInit(pvPostData);

  psa = SafeArrayCreateVector(VT_UI1, 0, cElems);
  if (!psa)
  {
    return E_OUTOFMEMORY;
  }

  hr = SafeArrayAccessData(psa, (LPVOID*)&pPostData);
  memcpy(pPostData, cszPostData, cElems);
  hr = SafeArrayUnaccessData(psa);

  V_VT(pvPostData) = VT_ARRAY | VT_UI1;
  V_ARRAY(pvPostData) = psa;
  return NOERROR;
}

1 个答案:

答案 0 :(得分:1)

我尝试重现您的示例,当我定义vHeader变量的类型时,它会起作用:

// Specify a binary Content-Type.
V_VT(&vHeaders) = VT_BSTR;
V_BSTR(&vHeaders) = SysAllocString(L"Content-Type: application/x-www-form-urlencoded\r\nContent-Encoding: UTF-8\r\n");

见这里:https://support.microsoft.com/en-us/kb/167658