 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Scott Grant Guest
|
Posted: Sat May 28, 2005 11:49 pm Post subject: Posting data to a web page |
|
|
Hi,
I'm trying get some communications up and running between an app (built
in BCB5) and a PHP based web page. I'm going to need to log a user in,
and then transfer up some data (<1 meg or so). I've built a little test
project, which is falling at the first hurdle - posting (any) data
(atall) to the web page.
I've tried two methods, using an NMHTTP component, and using the
TCppWebBrowser control. Neither of which seem to be posting the data.
The web page itself just enumerates and prints the $_POST array (and the
$_GET array - just in case), this snippet is the from the first few
lines of the page:
$postinfo="";
$postinfo.="Post total is ".count($_POST)."
";
foreach($_POST as $key=>$buf){
$postinfo.="$key = $buf
";
}
Later on, in the html body,I print $postinfo. The same gets done for
$_GET...
This bit seems to work fine, adding a wee form to the page allows me to
see that the Post data is being correctly read.
Attempt #1 involved using the NMHTTP component.
NMHTTP1->Post(CBURL->Text,"PostData=1");
where CBURL contains the url. Altering the text string doesn't
seem to matter... The component correctly navigates to the URL, but no
post data gets caught be the script, which outputs 'Post total is 0'
So it was back to FireFox & Google, for a look at TCppWebBrowser. After
much searching I ended up at
http://bdn.borland.com/article/0,1410,27519,00.html , where I found an
excellent article - thanks Adam Vieweger! Only it produces the same
results - ie I get to the web page OK, but 'Post total is 0'. Here's the
code I copied from the article : pretty much as is appart from changing
rn in the header to /r/n - which seemed appropriate...
void TForm2::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-
urlencoded/r/n");
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);
}
Am I missing something? This is my first attempt at talking to a web
page, so I'm now reduced to hacking the same bits of code over and over
in the blind hope that something will suddenly change...
Can anyone save me from pulling out the rest of my hair and getting a
divorce?
Thanks!
Scott
|
|
| Back to top |
|
 |
Sydney Delieu Guest
|
Posted: Sun Sep 04, 2005 12:43 pm Post subject: Re: Posting data to a web page |
|
|
Hi Scott,
I have tried this using NMHTTP component, but I found many problems
doing it this way. I have never tried using a TCPPWebBrowser because I
found using the TIdHTTP component (Indy) works very well.
Example PHP file:
<?php
print_r($_POST); // print the POST array.
?>
Then create BCB project, drop on a TIdHTTP component (called 'http' in
below example), then add OnClick event handler for a button:
void __fastcall TForm1::cmdPostDataClick(TObject *Sender)
{
AnsiString s, url;
TStringList *sl;
try {
url = txtAddress->Text;
sl = new TStringList;
sl->Add("username=SomeUserName Entered");
sl->Add("somevar=Some Variable");
s = http->Post(url, sl);
Memo1->Lines->Text = s;
}
__finally {
delete sl;
}
}
The Memo1 should display the following:
Array
(
[username] => SomeUserName Entered
[somevar] => Some Variable
)
Hope that this helps.
Regards,
Syd
Scott Grant wrote:
| Quote: | Hi,
I'm trying get some communications up and running between an app (built
in BCB5) and a PHP based web page. I'm going to need to log a user in,
and then transfer up some data (<1 meg or so). I've built a little test
project, which is falling at the first hurdle - posting (any) data
(atall) to the web page.
I've tried two methods, using an NMHTTP component, and using the
TCppWebBrowser control. Neither of which seem to be posting the data.
The web page itself just enumerates and prints the $_POST array (and the
$_GET array - just in case), this snippet is the from the first few
lines of the page:
$postinfo="";
$postinfo.="Post total is ".count($_POST)."
";
foreach($_POST as $key=>$buf){
$postinfo.="$key = $buf
";
}
Later on, in the html body,I print $postinfo. The same gets done for
$_GET...
This bit seems to work fine, adding a wee form to the page allows me to
see that the Post data is being correctly read.
Attempt #1 involved using the NMHTTP component.
NMHTTP1->Post(CBURL->Text,"PostData=1");
where CBURL contains the url. Altering the text string doesn't
seem to matter... The component correctly navigates to the URL, but no
post data gets caught be the script, which outputs 'Post total is 0'
So it was back to FireFox & Google, for a look at TCppWebBrowser. After
much searching I ended up at
http://bdn.borland.com/article/0,1410,27519,00.html , where I found an
excellent article - thanks Adam Vieweger! Only it produces the same
results - ie I get to the web page OK, but 'Post total is 0'. Here's the
code I copied from the article : pretty much as is appart from changing
rn in the header to /r/n - which seemed appropriate...
void TForm2::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-
urlencoded/r/n");
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);
}
Am I missing something? This is my first attempt at talking to a web
page, so I'm now reduced to hacking the same bits of code over and over
in the blind hope that something will suddenly change...
Can anyone save me from pulling out the rest of my hair and getting a
divorce?
Thanks!
Scott
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|