Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to post data without Indy or another 3rd party component

Status
Not open for further replies.

Nordlund

Programmer
Jul 17, 2001
458
SE
Hi friends.
How do I post http data without using Indy or another 3rd party components?

I have to use WinInet to perform this.

I'm using Turbo Explorer, and this version does not allow me to add 3rd party components...


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-"There is always another way to solve it, but I prefer my way.
 
I want to invoke a TImage with the Paypal logo, and when clicking the Image, the standard Web browsern should be started and redirected to paypal with the encrypted post data. How du I proceed?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-"There is always another way to solve it, but I prefer my way.
 
You could try CreateProcess to launch the IEXPLORE.EXE with the parameter containing the website addy.

Check out faq102-6048 and faq102-5462 for help on that.
 
Hi.
No luck with this faq's.

I've been able to use Indy, even when Turbo explorer doesn't support 3rd party components.

Anymore ideas anyone??

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-"There is always another way to solve it, but I prefer my way.
 
He who search should find...!

I have found a solution.
Look at this example. This will send a POSTDATA to a server with two FORM names called postvar1 and postvar2

Add this uses:
Code:
uses
  ComObj,

Add this:
Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  IEApp: OLEVariant;
  HeaderStr, EncodedStr: string;
  Post: OleVariant;
  nIdx: Integer;
begin
  // Set headerstring
  HeaderStr := 'Content-Type: application/x-[URL unfurl="true"]www-form-urlencoded'[/URL] + #10#13;

  // Make the post string URL encoded
  EncodedStr := 'postvar1=answer&postvar2=anotheranswer';

  // The post must be an array. But without null terminator (-1)
  Post := VarArrayCreate([0, Length(EncodedStr) - 1], varByte);

  // Put Post in array
  for nIdx := 1 to Length(EncodedStr) do
    Post[nIdx - 1] := Ord(EncodedStr[nIdx]);

  IEApp         := CreateOLEObject('InternetExplorer.Application');
  IEApp.Visible := True;
//  IEApp.Top     := 0;
//  IEApp.Left    := 0;
//  IEApp.Width   := Screen.Width;
//  IEApp.Height  := Screen.Height;
  IEApp.Navigate('[URL unfurl="true"]http://127.0.0.1/answer.asp','','',Post,[/URL] HeaderStr);
end;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-"There is always another way to solve it, but I prefer my way.
 
You might have to encode the postdata by using HTTPEncode (HttpApp unit).

Code:
EncodedStr := 'postvar1=answer&postvar2='+HttpEncode(strPostanswer2);

...or the whole EncodedStr variable... I'm a little bit unsure about that.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-"There is always another way to solve it, but I prefer my way.
 
Please, excuse me the disgression, but I'm starting to feel ill seeing that bad commented code invading the web.

>>
// The post must be an array. But without null terminator (-1)
Post := VarArrayCreate([0, Length(EncodedStr) - 1], varByte);
<<

The comment is totally wrong. AnsiString (strings) have not a null terminator counted in the length. The Length(EncodedStr) - 1 the programmer is using is due to VarArrayCreate calling for the upper bounds of the zero-based array (and not for the item quantity like SetLength).

From the Delphi Help:

A := VarArrayCreate([0, 4], varVariant);
A[0] := 1;
A[1] := 1234.5678;
A[2] := 'Hello world';
A[3] := True;
A[4] := VarArrayOf([1, 10, 100, 1000]);


Using [0, 4] you have ONE dimension (0+1) of FIVE items (4+1) indexed from zero to four.

That is why you need to pass Length(...) - 1.

Compare with SetLength, where SetLength(A, 4) creates an array of FOUR items, indexed from zero to three.

The programmer was "guessing" there. He found that Length(...) was not working, tryed Length(...) - 1 and happily decided that was due to a "null terminator". A simple RTFM will have solved the issue.

Sorry for the (over) reaction. I hate wrong comments, I feel they are worse that not comments at all.

buho (A).
 
Now to your question:

Lets say we have an html form with two fields txtData1 and txtData2 and a submit button.

We manually fire the explorer, fill the fields with "año" and "blah blah" and click the submit button.

And we get:

Code:
HEADERS:
HTTP_ACCEPT:image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/msword, */*
HTTP_ACCEPT_LANGUAGE:en-us 
HTTP_CONNECTION:Keep-Alive 
HTTP_HOST:[URL unfurl="true"]www.2k.klkx.lan[/URL] HTTP_REFERER:[URL unfurl="true"]http://www.2k.klkx.lan/_develop/post_to_navheaders.htm[/URL] 
HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) 
HTTP_CONTENT_LENGTH:47 
HTTP_CONTENT_TYPE:application/x-[URL unfurl="true"]www-form-urlencoded[/URL] HTTP_ACCEPT_ENCODING:gzip, deflate 
HTTP_CACHE_CONTROL:no-cache 

POST:
txtData1=a%F1o&txtData2=blah+blah&Submit=Submit

The post above is what we need to produce with our code, as it is what the server receives in the "normal" situation.

Now we use our code with:
EncodedStr := 'txtData1=año&txtData2=blah blah';

Code:
HEADERS:
HTTP_ACCEPT:*/* 
HTTP_ACCEPT_LANGUAGE:en-us 
HTTP_CONNECTION:Keep-Alive 
HTTP_HOST:[URL unfurl="true"]www.2k.klkx.lan[/URL] 
HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) HTTP_COOKIE:ASPSESSIONIDAQQSSTTS=KDELADOBJEHNPJBMJADMDNJG HTTP_CONTENT_LENGTH:31 
HTTP_CONTENT_TYPE:application/x-[URL unfurl="true"]www-form-urlencoded[/URL] HTTP_ACCEPT_ENCODING:gzip, deflate 
HTTP_CACHE_CONTROL:no-cache 

POST:
txtData1=año&txtData2=blah blah

Next with:
EncodedStr := HTTPEncode(EncodedStr);

Code:
HEADERS:
HTTP_ACCEPT:*/* 
HTTP_ACCEPT_LANGUAGE:en-us 
HTTP_CONNECTION:Keep-Alive HTTP_HOST:[URL unfurl="true"]www.2k.klkx.lan[/URL] HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) HTTP_COOKIE:ASPSESSIONIDAQQSSTTS=KDELADOBJEHNPJBMJADMDNJG HTTP_CONTENT_LENGTH:39 
HTTP_CONTENT_TYPE:application/x-[URL unfurl="true"]www-form-urlencoded[/URL] HTTP_ACCEPT_ENCODING:gzip, deflate 
HTTP_CACHE_CONTROL:no-cache 

POST:
txtData1%3Da%F1o%26txtData2%3Dblah+blah

And finally with:
EncodedStr := 'txtData1=' + HTTPEncode('año') + '&txtData2=' + HTTPEncode('blah blah');

Code:
HEADERS:
HTTP_ACCEPT:*/* 
HTTP_ACCEPT_LANGUAGE:en-us 
HTTP_CONNECTION:Keep-Alive HTTP_HOST:[URL unfurl="true"]www.2k.klkx.lan[/URL] HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) HTTP_COOKIE:ASPSESSIONIDAQQSSTTS=KDELADOBJEHNPJBMJADMDNJG HTTP_CONTENT_LENGTH:33 
HTTP_CONTENT_TYPE:application/x-[URL unfurl="true"]www-form-urlencoded[/URL] HTTP_ACCEPT_ENCODING:gzip, deflate 
HTTP_CACHE_CONTROL:no-cache 

POST:
txtData1=a%F1o&txtData2=blah+blah

So EncodedStr := 'txtData1=' + HTTPEncode(Value1) + '&txtData2=' + HTTPEncode(Value2); produces a string like the one sent in the "normal" situation.

buho (A).
 
Yes, I know. I've seen it like five time in two different forums in the last two weeks. :)

buho (A).
 
What can i say but LOL. :)


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-"There is always another way to solve it, but I prefer my way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top