Giving MainForm as the Owner in TFrm1.Create will include the newly created form in the components list of MainForm. A component ensures that this list doesn't contain any two components with the same non-empty name (otherwise FindComponent won't work). This mechanism also works when a component changes its name.
As long as you don't specify the name in TFrm1.Create it is most likely that it is set by the LoadFromFile method, which means that you don't have much influence on the name unless you change the file's content.
A valid workaround is to create the form with nil as Owner, load the form from the file, change the name to a unique value or to an empty string and finally call MainForm.InsertComponent.
procedure MyProcedure1;
var MyFrm: TFrm1;
begin
...
MyFrm:= TFrm1.create(nil);
MyFrm.BringToFront;
MyFrm.LoadFromFile(someFile);
MyFrm.Name := ''; // or some unique name
MainForm.InsertComponent(MyFrm);
end;
First of all I would start with adding Fiddler2 and SoapUI to your arsenal of tools when dealing with soap and web services.
Sounds like the envelope header is incomplete. In my experience with D2007 and a WCF web service, Delphi would not build the envelope header correctly, so I had to intercept the call and add the items like this:
procedure TMainForm.MyMessageEnvelopeComplete(Sender: TROSOAPMessage);
begin
Sender.EnvelopeNode.AddAttribute('xmlns:a', 'http://www.w3.org/2005/08/addressing', False);
Sender.HeaderNode.Add('a:Action').Value := 'http://Testservice.Connect/IConnect/Ping';
Sender.HeaderNode.Add('a:To').Value := 'http://testservice-pc:2021/WSConnect';
end;
EDIT:
If you want to build the soap envelope and the post command by hand, I have done it using the following code...
procedure TMainForm.Button5Click(Sender: TObject);
procedure HandleError(const errorCode: integer);
var
errorMessage: AnsiString;
begin
SetLength(errorMessage, 256);
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_FROM_HMODULE,
Pointer(GetModuleHandle('wininet.dll')),
errorCode, 0, PChar(errorMessage), Length(errorMessage), nil);
SetLength(errorMessage, StrLen(PChar(errorMessage)));
raise Exception.Create(errorMessage);
end;
function BuildHeader: TStringStream;
begin
result := TStringStream.Create('');
try
result.WriteString('Content-Type: application/soap+xml;charset=UTF-8;action="http://Thermo.Connect/IHCSConnect/Ping"' + sLineBreak);
except
result.Free;
raise;
end;
end;
function BuildBody: TStringStream;
begin
result := TStringStream.Create('');
with result do
try
WriteString('<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">' + sLineBreak);
WriteString('<s:Header>' + sLineBreak);
WriteString('<a:Action>http://Thermo.Connect/IHCSConnect/Ping</a:Action>' + sLineBreak);
WriteString('<a:To>http://thermo-pc:2021/WSHCSConnect</a:To>' + sLineBreak);
WriteString('</s:Header>' + sLineBreak);
WriteString('<s:Body>' + sLineBreak);
WriteString('<Ping xmlns="http://Thermo.Connect">' + sLineBreak);
WriteString('</Ping>' + sLineBreak);
WriteString('</s:Body>' + sLineBreak);
WriteString('</s:Envelope>' + sLineBreak);
except;
result.Free;
raise;
end;
end;
var
InetRoot: HINTERNET;
InetConnect: HINTERNET;
Request: HINTERNET;
begin
InetRoot := InternetOpen('GabeCode', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
try
InetConnect := InternetConnect( InetRoot, 'thermo-pc:2021', 0, '',
'', INTERNET_SERVICE_HTTP, 0, Cardinal(Self));
try
Request := HttpOpenRequest( InetConnect, 'POST', 'WSHCSConnect', 'HTTP/1.1', nil, nil,
INTERNET_FLAG_KEEP_CONNECTION or INTERNET_FLAG_NO_CACHE_WRITE,
0);
try
// build add header items to the post request
with BuildHeader do
try
HttpAddRequestHeaders(Request, PChar(DataString), Length(DataString), HTTP_ADDREQ_FLAG_ADD);
finally
Free;
end;
// build the body of data being posted and send the post
with BuildBody do
try
if not HttpSendRequest(Request, nil, 0, PChar(DataString), Length(DataString)) then
HandleError(GetLastError);
finally
Free;
end;
finally
InternetCloseHandle(Request);
end;
finally
InternetCloseHandle(InetConnect);
end;
finally
InternetCloseHandle(InetRoot);
end;
end;
Best Answer