mgazza
asked on
WiniNet
id like to read and write raw data from my web site but i cant find any examples of this i know how to read the file but writting the file is another problem i get an error with this method of httpUpload the httpDownload works fine.
can some person write the code for saveing data to a file on the internet
httpupload returns an error
procedure HTTPUpload(Remote:String;D ata:TMemor yStream);
var create,file_remote_handle: Phandle;
Data_written:cardinal;
buffer:array[0..512] of char;
begin
create := InternetOpen('Mozilla/4.0 (compatible)', INTERNET_OPEN_TYPE_PRECONF IG , NIL, NIL, 0);
file_remote_handle:=Intern etOpenUrl( create, pchar(remote), NiL, 0, INTERNET_FLAG_RAW_DATA, 0);
if file_remote_handle<>nil then begin
repeat
FillChar(buffer,sizeof(buf fer),#0);
data.ReadBuffer(buffer,siz eof(buffer ));
InternetWriteFile(file_rem ote_handle ,addr(buff er),sizeof (buffer),D ata_Writte n);
until (Data_Written<=0)or (data.Position>=data.Size) ;
end//End of IternetOPenUrl
else begin
MessageBox(0,'Could Not Resolve Host!','Error',0);
end;
end;
procedure HTTPDownload(Remote:String ;Data:TMem oryStream) ;
var create,file_remote_handle: Phandle;
Data_written:cardinal;
buffer:array[0..512] of char;
begin
create := InternetOpen('Mozilla/4.0 (compatible)', INTERNET_OPEN_TYPE_PRECONF IG , NIL, NIL, 0);
file_remote_handle:=Intern etOpenUrl( create, pchar(remote), NiL, 0, INTERNET_FLAG_RAW_DATA, 0);
if file_remote_handle<>nil then begin
repeat
FillChar(buffer,sizeof(buf fer),#0);
//wininet.InternetWriteFil e()
InternetReadFile(file_remo te_handle, addr(buffe r),sizeof( buffer),Da ta_Written );
data.Writebuffer(buffer,si zeof(buffe r));
until Data_Written<=0;
end//End of IternetOPenUrl
else begin
MessageBox(0,'Could Not Resolve Host!','Error',0);
end;
end;
procedure TForm1.DownLoadButton1Clic k(Sender: TObject);
var
smem:tmemorystream;
begin
smem:=tmemorystream.Create ;
httpdownload(edit1.Text,sm em);
memo1.Text:=pchar(smem);
end;
can some person write the code for saveing data to a file on the internet
httpupload returns an error
procedure HTTPUpload(Remote:String;D
var create,file_remote_handle:
Data_written:cardinal;
buffer:array[0..512] of char;
begin
create := InternetOpen('Mozilla/4.0 (compatible)', INTERNET_OPEN_TYPE_PRECONF
file_remote_handle:=Intern
if file_remote_handle<>nil then begin
repeat
FillChar(buffer,sizeof(buf
data.ReadBuffer(buffer,siz
InternetWriteFile(file_rem
until (Data_Written<=0)or (data.Position>=data.Size)
end//End of IternetOPenUrl
else begin
MessageBox(0,'Could Not Resolve Host!','Error',0);
end;
end;
procedure HTTPDownload(Remote:String
var create,file_remote_handle:
Data_written:cardinal;
buffer:array[0..512] of char;
begin
create := InternetOpen('Mozilla/4.0 (compatible)', INTERNET_OPEN_TYPE_PRECONF
file_remote_handle:=Intern
if file_remote_handle<>nil then begin
repeat
FillChar(buffer,sizeof(buf
//wininet.InternetWriteFil
InternetReadFile(file_remo
data.Writebuffer(buffer,si
until Data_Written<=0;
end//End of IternetOPenUrl
else begin
MessageBox(0,'Could Not Resolve Host!','Error',0);
end;
end;
procedure TForm1.DownLoadButton1Clic
var
smem:tmemorystream;
begin
smem:=tmemorystream.Create
httpdownload(edit1.Text,sm
memo1.Text:=pchar(smem);
end;
ASKER
yes i tryed this using the internetconnect func i do have a username and password to my own ftp/http space and a server running there
ASKER
what is the procedure for connection to my server and wring some data to a file????
For http uploads the functions you have to call are
InternetOpen
InternetConnect
HTTPOpenRequest
InternetWriteFile
HTTPCloseRequest
InternetOpen
InternetConnect
HTTPOpenRequest
InternetWriteFile
HTTPCloseRequest
Version tested working
procedure TForm1.HTTPUpload(Remote:P Char;Data: TMemoryStr eam);
var
iFileBuffer,iRetBuffer : INTERNET_BUFFERS;
iConnect : INTERNET_CONNECTED_INFO;
P : array [0..512] of char;
iResult:boolean;
myURLComp : TUrlComponents;
iConnectHandle,create : PHandle;
requestHandle,file_remote_ handle:PHa ndle;
myFlags,Data_written:cardi nal;
byteswritten : integer;
buffer:array[0..512] of char;
begin
FillChar(iFileBuffer,sizeo f(iFileBuf fer),#0);
iFileBuffer.dwStructSize:= sizeof(INT ERNET_BUFF ERS);
iFileBuffer.Next:=nil;
iFileBuffer.lpcszHeader:=n il;
iFileBuffer.dwHeadersLengt h:=0;
iFileBuffer.dwHeadersTotal :=0;
iFileBuffer.lpvBuffer:=@bu ffer;
iFileBuffer.dwBufferLength :=data.siz e;
iFileBuffer.dwBufferTotal: =data.size ;
iFileBuffer.dwOffsetLow:=0 ;
iFileBuffer.dwOffsetHigh:= 0;
FillChar(myURLComp,sizeof( TUrlCompon ents),#0);
myURLComp.dwStructSize:=si zeof(TURLC omponents) ;
myURLComp.lpszScheme:=nil;
myURLComp.dwSchemeLength:= 0;
myURLComp.nScheme:=0;
myURLComp.lpszHostName:=pc har('192.1 68.254.101 ');
myURLComp.dwHostNameLength :=16;
myURLComp.nPort:=80;
myURLComp.lpszUserName:=pc har('sruse r');
myURLComp.dwUserNameLength :=7;
myURLComp.lpszPassword:=pc har('sruse r');
myURLComp.dwPasswordLength :=7;
myURLComp.lpszUrlPath:=pch ar('');
myURLComp.dwUrlPathLength: =32;
myURLComp.lpszExtraInfo:=n il;
myURLComp.dwExtraInfoLengt h:=0;
create := InternetOpen('Mozilla/4.0 (compatible)', INTERNET_OPEN_TYPE_PRECONF IG , NIL, NIL, 0);
memo1.lines.add('OpenHandl e='+inttoh ex(longint (create),8 )+#13);
iConnectHandle:=InternetCo nnect(crea te,pchar(' 192.168.25 4.101'),80 ,pchar('sr user'),pch ar('sruser '),INTERNE T_SERVICE_ HTTP,0,0);
memo1.lines.add('ConnectHa ndle='+int tohex(long int(iconne cthandle), 8)+#13);
// create the file on the host
requestHandle:=HttpOpenReq uestA(iCon NectHandle ,pchar('PU T'),pchar( 'myFile.tx t'),nil,pc har('myfil e.txt'),ni l,INTERNET _FLAG_NEED _FILE,0);
memo1.lines.add('RequestHa ndle='+int tohex(long int(reques tHandle),8 )+#13);
bytesWritten:=sizeof(buffe r);
while bytesWritten>=sizeof(buffe r) do begin
bytesWritten:=data.Read(bu ffer,sizeo f(buffer)) ; //sizeof(buffer));
iFileBuffer.dwBufferLength :=bytesWri tten;
iFileBuffer.dwBufferTotal: =bytesWrit ten;
if HttpSendRequestEx(requestH andle,@iFi leBuffer,n il,0,0) then memo1.lines.add('SendReque st Ok'+#13);
memo1.lines.add('Write Size='+inttostr(bytesWritt en));
end;
InternetCloseHandle(reques tHandle);
InternetCloseHandle(iConne ctHandle);
InternetCloseHandle(create );
end;
procedure TForm1.UploadClick(Sender: TObject);
var
A: array[0..79] of Char;
smem:tmemorystream;
begin
StrPCopy(A, Edit2.Text);
smem:=tmemorystream.Create ;
smem.LoadFromFile('default .htm');
httpupload(A,smem);
end;
procedure TForm1.HTTPUpload(Remote:P
var
iFileBuffer,iRetBuffer : INTERNET_BUFFERS;
iConnect : INTERNET_CONNECTED_INFO;
P : array [0..512] of char;
iResult:boolean;
myURLComp : TUrlComponents;
iConnectHandle,create : PHandle;
requestHandle,file_remote_
myFlags,Data_written:cardi
byteswritten : integer;
buffer:array[0..512] of char;
begin
FillChar(iFileBuffer,sizeo
iFileBuffer.dwStructSize:=
iFileBuffer.Next:=nil;
iFileBuffer.lpcszHeader:=n
iFileBuffer.dwHeadersLengt
iFileBuffer.dwHeadersTotal
iFileBuffer.lpvBuffer:=@bu
iFileBuffer.dwBufferLength
iFileBuffer.dwBufferTotal:
iFileBuffer.dwOffsetLow:=0
iFileBuffer.dwOffsetHigh:=
FillChar(myURLComp,sizeof(
myURLComp.dwStructSize:=si
myURLComp.lpszScheme:=nil;
myURLComp.dwSchemeLength:=
myURLComp.nScheme:=0;
myURLComp.lpszHostName:=pc
myURLComp.dwHostNameLength
myURLComp.nPort:=80;
myURLComp.lpszUserName:=pc
myURLComp.dwUserNameLength
myURLComp.lpszPassword:=pc
myURLComp.dwPasswordLength
myURLComp.lpszUrlPath:=pch
myURLComp.dwUrlPathLength:
myURLComp.lpszExtraInfo:=n
myURLComp.dwExtraInfoLengt
create := InternetOpen('Mozilla/4.0 (compatible)', INTERNET_OPEN_TYPE_PRECONF
memo1.lines.add('OpenHandl
iConnectHandle:=InternetCo
memo1.lines.add('ConnectHa
// create the file on the host
requestHandle:=HttpOpenReq
memo1.lines.add('RequestHa
bytesWritten:=sizeof(buffe
while bytesWritten>=sizeof(buffe
bytesWritten:=data.Read(bu
iFileBuffer.dwBufferLength
iFileBuffer.dwBufferTotal:
if HttpSendRequestEx(requestH
memo1.lines.add('Write Size='+inttostr(bytesWritt
end;
InternetCloseHandle(reques
InternetCloseHandle(iConne
InternetCloseHandle(create
end;
procedure TForm1.UploadClick(Sender:
var
A: array[0..79] of Char;
smem:tmemorystream;
begin
StrPCopy(A, Edit2.Text);
smem:=tmemorystream.Create
smem.LoadFromFile('default
httpupload(A,smem);
end;
ASKER
that looks gud ill get back 2 u as soon as i have time to check it :D
ASKER
humm it doesnt seem to work over here :S
the packets send how do i get my ip for my server i have no idea how www.mgazzasoft.150m.com
the packets send how do i get my ip for my server i have no idea how www.mgazzasoft.150m.com
ASKER
ok the internet connect loged in ok (connect <>nil and request <>nil) humm how about the request is put a real value?
HttpOpenRequestA(iConNectH andle,pcha r('PUT'),p char('myFi le.txt'),n il,pchar(' myfile.txt '),nil,INT ERNET_FLAG _NEED_FILE ,0);
do i have to put a url infont of the file names? and why 2 file names?
HttpOpenRequestA(iConNectH
do i have to put a url infont of the file names? and why 2 file names?
ASKER
requestex gets failed
If you are getting a handle returned from the connect request then this part is working fine.
I just used local filnames to test this , you need the whole URL with filename in the first filename . The second filename represents the referrer , you can put the url in here or leave it as null.
If this function does not return a valid handle then the following code will not work.
I just used local filnames to test this , you need the whole URL with filename in the first filename . The second filename represents the referrer , you can put the url in here or leave it as null.
If this function does not return a valid handle then the following code will not work.
ASKER
so it would be
HttpOpenRequestA(iConNectH andle,pcha r('PUT'),p char('http://www.mgazzsoft.150m.com/myFile.txt'),nil,pchar('myfile.txt'),nil,INTERNET_FLAG_NEED_FILE,0);
? please change this if it is wrong
HttpOpenRequestA(iConNectH
? please change this if it is wrong
ASKER
cant i use the ftp open file?
I think you shout put the following
requestHandle:=HttpOpenReq uestA(iCon NectHandle ,pchar('PU T'),
pchar('myFile.txt'),nil,ni l,nil,INTE RNET_FLAG_ NEED_FILE, 0);
which will put the file into the root directory of the webserver.
put mgazzsoft.150m.com into the lpzHostName and
pcar('/') into lpszUrlPath in the urlcomponent
You can't use ftp with these calls as they are for uploading using http port 80.
Ftp could be used but you would have to ensure that the ftp server is active and its root directory is pointing to the web server root directory.
I think what you are doing should work
requestHandle:=HttpOpenReq
pchar('myFile.txt'),nil,ni
which will put the file into the root directory of the webserver.
put mgazzsoft.150m.com into the lpzHostName and
pcar('/') into lpszUrlPath in the urlcomponent
You can't use ftp with these calls as they are for uploading using http port 80.
Ftp could be used but you would have to ensure that the ftp server is active and its root directory is pointing to the web server root directory.
I think what you are doing should work
ASKER
the url conponent isnt even used any ware and im still not writting a file :s
Sorry,
you are right the url components are not being used.
I will need to test this with a real server on the net.
Jim Duncan
you are right the url components are not being used.
I will need to test this with a real server on the net.
Jim Duncan
ASKER
sure ill get one some free webspace for testing perpouses
address: www.TestSpace.150m.com
username: TestSpace.150m.com
password: 445566
this site has 1mb file size 100mb of freespace
main page is index.html
use this for all tests regarding wininet
please dont abuse it anyone or ill get in trouble
p.s sorry i took so long replying this time i just got hit by a virus bomb
address: www.TestSpace.150m.com
username: TestSpace.150m.com
password: 445566
this site has 1mb file size 100mb of freespace
main page is index.html
use this for all tests regarding wininet
please dont abuse it anyone or ill get in trouble
p.s sorry i took so long replying this time i just got hit by a virus bomb
I have just tested it on another server and it works when I put the fileanem in the open request with a root folder i.e. /myfiles/test.txt
One other point , could be the username having dots in it and not being converted to web characters.
if you want to send secure details you should email me at jbduncan@talk21.com as as this site is open to everyone.
One other point , could be the username having dots in it and not being converted to web characters.
if you want to send secure details you should email me at jbduncan@talk21.com as as this site is open to everyone.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
regarding my email
i only need to write limmited amounts of test to a file on a server is there a more compattable way of wrighing this to a file using ftp? of is more complicated?
i only need to write limmited amounts of test to a file on a server is there a more compattable way of wrighing this to a file using ftp? of is more complicated?
ASKER
hi im sure that that code works now :D thanks alot ill email you with the test but the server cant store its ip om my current doman space ill have to code some cgi script so i can send a request to qwrite a new to file thanks alot!(when i get some new web space domain name ect.....!)
To upload data you need to use a vaslid username/password for the site in question which gives write access to the upload directory.