[Wylug-help] OT Delphi 2009 and http POST
Gary Stainburn
gary.stainburn at ringways.co.uk
Wed Jan 14 14:06:10 UTC 2009
Hi folks,
I've got a Delphi App to write which needs to upload data to my web server and
I have a few problems.
1) I haven't used Delphi since Delphi 3 over 15 years ago. All the old sites
no longer exist and googling for Delphi help is disappointing. Anyone got any
good pointers?
2) poor/few Examples and componants. I've had a look for examples I can crib
from, and componants I can install. I tried Alcinoe which looks great but
won't install, complaining about rtl100.bpl being missing.
I've had similar problems with other componants. As for examples, there
aren't many, and there are none that post data.
I've managed to know up this code based on two seperate examples, and changes
by me. Anyone know why this doesn't work? The request works fine, and the
generated html is downloaded, but the form data does not work.
function UploadFilesHttpPostNew(const URLstring,FileName: string; names,
values, nFiles, vFiles: array of string):Boolean ;
const
BufferSize = 1024;
Server = 'www1.ringways.co.uk';
var
hSession, hURL,hRequest: HInternet;
sAppName: string;
Buffer: array[1..BufferSize] of Byte;
BufferLen,httpContext: DWORD;
f: File;
strData, n, v, boundary: string;
Headers: string;
idx: Integer;
ms: TMemoryStream;
ss: TStringStream;
begin
if Length(names) <> Length(values) then
raise Exception.Create('UploadFilesHttpPostNew: Names and Values must have
the same length.') ;
if Length(nFiles) <> Length(vFiles) then
raise Exception.Create('UploadFilesHttpPostNew: FileNames and FileValues
must have the same length.') ;
// anything random that WILL NOT occur in the data.
boundary := '---------------------------123456789';
strData := 'Content-Type: multipart/form-data; boundary=' + boundary +
#13#10#13#10;
for idx := Low(names) to High(names) do
begin
n := names[idx];
v := values[idx];
strData := strData + '--' + boundary + #13#10 + 'Content-Disposition:
form-data; name="' + n + '"' + #13#10#13#10 + v + #13#10;
end;
for idx := Low(nFiles) to High(nFiles) do
begin
n := nFiles[idx];
v := vFiles[idx];
strData := strData + '--' + boundary + #13#10 + 'Content-Disposition:
form-data; name="' + n + '"; filename="' + v + '"' + #13#10;
if v = '' then
begin
strData := strData + 'Content-Transfer-Encoding: binary'+#13#10#13#10;
end
else
begin
if (CompareText(ExtractFileExt(v), '.JPG') = 0) or
(CompareText(ExtractFileExt(v), '.JPEG') = 0) then
begin
strData := strData + 'Content-Type: image/jpeg'#13#10#13#10;
end
else if (CompareText(ExtractFileExt(v), '.PNG') = 0) then
begin
strData := strData + 'Content-Type: image/x-png'#13#10#13#10;
end
else if (CompareText(ExtractFileExt(v), '.PDF') = 0) then
begin
strData := strData + 'Content-Type: application/pdf'#13#10#13#10;
end
else if (CompareText(ExtractFileExt(v), '.HTML') = 0) then
begin
end;
ms := TMemoryStream.Create;
try
ms.LoadFromFile(v) ;
ss := TStringStream.Create('') ;
try
ss.CopyFrom(ms, ms.Size) ;
strData := strData + ss.DataString + #13#10;
finally
ss.Free;
end;
finally
ms.Free;
end;
end;
end;
strData := strData + '--' + boundary + '--' + #13#10#0; // FOOTER
Form1.Memo1.Text := strData;
{ code taken from GetInetFileNew }
result := false;
sAppName := ExtractFileName(Application.ExeName) ;
hSession := InternetOpen(PChar(sAppName), INTERNET_OPEN_TYPE_PRECONFIG, nil,
nil, 0) ;
try
hURL :=
InternetConnect(hSession,pchar(Server),INTERNET_DEFAULT_HTTP_PORT,nil,nil,INTERNET_SERVICE_HTTP,0,httpContext);
if hURL = Nil then
raise Exception.Create('GetInetFileNew: Internet Connect failed.') ;
hRequest :=
httpOpenRequest(hURL,pchar('POST'),pchar('/handheld.html'),nil,nil,nil,INTERNET_FLAG_RELOAD,0);
if hURL = Nil then
raise Exception.Create('GetInetFileNew: httpOpenRequest failed.') ;
if not httpSendRequest(hRequest,nil,0,
pchar(strData),length(strData)) then
raise Exception.Create('GetInetFileNew: Send Request failed.') ;
try
AssignFile(f, FileName) ;
Rewrite(f,1) ;
repeat
InternetReadFile(hRequest, @Buffer, SizeOf(Buffer), BufferLen) ;
BlockWrite(f, Buffer, BufferLen)
until BufferLen = 0;
CloseFile(f) ;
result := True;
finally
InternetCloseHandle(hURL)
end
finally
InternetCloseHandle(hSession)
end
end; //UploadFilesHttpPostNew
--
Gary Stainburn
This email does not contain private or confidential material as it
may be snooped on by interested government parties for unknown
and undisclosed purposes - Regulation of Investigatory Powers Act, 2000
More information about the Wylug-help
mailing list