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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to construct web address from concatenated text and variables 2

Status
Not open for further replies.

Han777

Programmer
Dec 12, 2007
19
My need is simple. I need to send a URL to collective2 to place an order at a specific price defined by a variable in delphi. I know how to send the URL but how do I concatenate into the URL a variable containing the price? Here's what the urL looks like:

.com/cgi-perl/signal. mpl?cmd=signal&
systemid=1234&pw=abcd&instrument=future&action=BTO&quant=5&
symbol=@ESU6&limit=1000&duration=GTC&stoploss=900.50&profittarget=1200

Suppose the stoploss and profittarget values were in variables named mystop and mytarget. Can I concatenate something like this:

' .com/cgi-perl/signal. mpl?cmd=signal&
systemid=1234&pw=abcd&instrument=future&action=BTO&quant=5&
symbol=@ESU6&limit=1000&duration=GTC&stoploss=' + mystop + '&profittarget=' mytarget
 
Yes you can. However, you might want to watch out for the potential of sending a mal formed value - assuming a user can affect those variables. If they can't then you should be fine. You might need to do something like ..&stoploss='+floattostr(mystop)+'&profittarget... if your variable is not a string type.

 
I've never had to do something like this, but if I had to, I would first put together a small class, something like "TURLBuilder" originating from the idea of the QueryString in C#. Building it is one thing I won't get into at the moment, but in the end, it would work something like this...

Code:
function BuildURL: String;
var
  B: TURLBuilder;
begin
  B:= TURLBuilder.Create;
  try
    B.URL:= '[URL unfurl="true"]http://www.collective2.com/cgi-perl/signal.mpl';[/URL]
    B['cmd']:= 'signal';
    B['systemid']:= '1234';
    B['pw']:= 'abcd';
    B['instrument']:= 'future';
    B['action']:= 'BTO';
    B['qant']:= '5';
    B['symbol']:= '@ESU6';
    B['limit']:= '1000';
    B['duration']:= 'GTC';
    B['stoploss']:= mystop;
    B['profittarget']:= mytarget;
    Result:= B.FullURL;
  finally
    B.Free;
  end;

If you want more info on how to go about this, lemme know so I can throw together a sample class for you.

JD Solutions
 
It depends on precisely what your issues are. You can assemble the URL as a string, or there are several API functions that tend to address the issues behind URL processing.

For example:

InternetCreateURL
InternetCanonicalizeURL
InternetCombineURL
InternetCrackURL

There are also WinHTTP equivalents as well if you wish to go that route.


It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
(I forgot to put that last end; closing the procedure above)

As Glenn pointed out, there's usually already pre-made methods that can accomplish what you need, I just tend to reinvent the wheel all the time to make sure it's doing exactly what I want it to do.

JD Solutions
 
Thanks Gentlemen,

I'm using Delphi win32 (not .net)I'm hoping that Djangman's solution works as it is the simplest. So I'll try that first. If I understand correctly the URL would look like this, assuming the variables were double variables:

.com/cgi-perl/signal. mpl?cmd=signal&
systemid=1234&pw=abcd&instrument=future&action=BTO&quant=5&
symbol=@ESU6&limit=1000&duration=GTC&stoploss='+DoubleToStr (mystop)+'&profittarget='+DoubleToStr (mytarget);

Is that right for Delphi?
 
You may wish to use FormatFloat instead...

Code:
....GTC&stoploss='+FormatFloat('0.0', mystop)+'&....

It generally helps control how the string is output, instead of guessing and hoping it's formatted properly.

JD Solutions
 
Thanks djjd47130. Since you used the word "instead" of DoubleToStr am I correct to assume that FormatFloat produces a string output from a double formatted variable like mystop?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top