線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2937
推到 Plurk!
推到 Facebook!

CPPWebBrowser的PostData

 
axsoft
版主


發表:681
回覆:1056
積分:969
註冊:2002-03-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-02-26 17:01:48 IP:61.218.xxx.xxx 未訂閱
CPPWebBrowser的PostData    作者:雨中漫步    http://www.chinabcb.com    WideString URL = "http://www.mmmnn.com/maker/pass.asp";; ParamString = "username=test&password=12345"; TVariant *PostData = new TVariant(ParamString); CPPWebBrowser->Navigate(URL,0,0,PostData,0);     這樣是不行的,網頁收不到參數。 http://www.mmmnn.com/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);    // *Method 2* TVariant vtEmpty; char *str = "acti        聯盟----Visita網站http://www.vista.org.tw ---[ 發問前請先找找舊文章 ]---
avex
初階會員


發表:19
回覆:49
積分:43
註冊:2003-03-28

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-09-18 11:10:40 IP:218.163.xxx.xxx 未訂閱
這篇文章好像尾巴斷了, 不完整, 可否請原作者補齊.... 謝謝~~
xfile
初階會員


發表:21
回覆:80
積分:25
註冊:2004-10-02

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-01-12 21:14:46 IP:220.130.xxx.xxx 未訂閱
引言: 這篇文章好像尾巴斷了, 不完整, 可否請原作者補齊.... 謝謝~~ < face="Verdana, Arial, Helvetica"> 這篇也是轉貼過來的,原文在此
How to post data using TCppWebBrowser in C++Builder 
Rating:      Ratings: 16     Rate it    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);    // *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 < strlen(str); n++){
    uint_array[n]=str[n];
  }
 
  SAFEARRAY* sa = uint_array.Detach();
  SafeArrayCopy(sa, &uint_array);      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 are pretty good code, but they suffer from the same flaw -- they don't work! Both pass the data correctly, but Navigate still did not want to post the data to the specified URL.    DIGGING DEEPER
I headed back to MSDN for more research on WebBrowser capabilities. And I found two articles that helped: Q167658 and Q165800. With that information in hand, I was finally successful!    Here's what I wound up with. This code demonstrates how to use a CppWebBrowser component for browsing and for posting HTTP data. You can easily modify this code for use in your own applications.    void 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);
}
I have no reason to think this code is perfect. But it does have a unique advantage over my previous efforts: It works!    I hope it helps you master C--WebBrowser with C++Builder.    --Adam Vieweger    
系統時間:2024-05-06 1:32:51
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!