Link to home
Start Free TrialLog in
Avatar of mgazza
mgazzaFlag for United Kingdom of Great Britain and Northern Ireland

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;Data:TMemoryStream);
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_PRECONFIG , NIL, NIL, 0);
file_remote_handle:=InternetOpenUrl(create, pchar(remote), NiL, 0, INTERNET_FLAG_RAW_DATA, 0);
if file_remote_handle<>nil then begin

        repeat
                FillChar(buffer,sizeof(buffer),#0);
                data.ReadBuffer(buffer,sizeof(buffer));
                InternetWriteFile(file_remote_handle,addr(buffer),sizeof(buffer),Data_Written);

        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:TMemoryStream);
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_PRECONFIG , NIL, NIL, 0);
file_remote_handle:=InternetOpenUrl(create, pchar(remote), NiL, 0, INTERNET_FLAG_RAW_DATA, 0);
if file_remote_handle<>nil then begin

        repeat
                FillChar(buffer,sizeof(buffer),#0);
                //wininet.InternetWriteFile()
                InternetReadFile(file_remote_handle,addr(buffer),sizeof(buffer),Data_Written);
                data.Writebuffer(buffer,sizeof(buffer));

        until Data_Written<=0;

end//End of IternetOPenUrl
else begin
MessageBox(0,'Could Not Resolve Host!','Error',0);
end;

end;


procedure TForm1.DownLoadButton1Click(Sender: TObject);
var
smem:tmemorystream;
begin
        smem:=tmemorystream.Create;
        httpdownload(edit1.Text,smem);
        memo1.Text:=pchar(smem);
end;
Avatar of JDuncan
JDuncan
Flag of United Kingdom of Great Britain and Northern Ireland image

Web sites are usually set to read only access and requests are made using the anonymous login name.
To upload data you need to use a vaslid username/password for the site in question which gives write access to the upload directory.
Avatar of mgazza

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
Avatar of mgazza

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

Version tested working

procedure TForm1.HTTPUpload(Remote:PChar;Data:TMemoryStream);
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:PHandle;
  myFlags,Data_written:cardinal;
  byteswritten : integer;
  buffer:array[0..512] of char;
begin
  FillChar(iFileBuffer,sizeof(iFileBuffer),#0);
  iFileBuffer.dwStructSize:=sizeof(INTERNET_BUFFERS);
  iFileBuffer.Next:=nil;
  iFileBuffer.lpcszHeader:=nil;
  iFileBuffer.dwHeadersLength:=0;
  iFileBuffer.dwHeadersTotal:=0;
  iFileBuffer.lpvBuffer:=@buffer;
  iFileBuffer.dwBufferLength:=data.size;
  iFileBuffer.dwBufferTotal:=data.size;
  iFileBuffer.dwOffsetLow:=0;
  iFileBuffer.dwOffsetHigh:=0;

  FillChar(myURLComp,sizeof(TUrlComponents),#0);
  myURLComp.dwStructSize:=sizeof(TURLComponents);
  myURLComp.lpszScheme:=nil;
  myURLComp.dwSchemeLength:=0;
  myURLComp.nScheme:=0;
  myURLComp.lpszHostName:=pchar('192.168.254.101');
  myURLComp.dwHostNameLength:=16;
  myURLComp.nPort:=80;
  myURLComp.lpszUserName:=pchar('sruser');
  myURLComp.dwUserNameLength:=7;
  myURLComp.lpszPassword:=pchar('sruser');
  myURLComp.dwPasswordLength:=7;
  myURLComp.lpszUrlPath:=pchar('');
  myURLComp.dwUrlPathLength:=32;
  myURLComp.lpszExtraInfo:=nil;
  myURLComp.dwExtraInfoLength:=0;

  create := InternetOpen('Mozilla/4.0 (compatible)', INTERNET_OPEN_TYPE_PRECONFIG , NIL, NIL, 0);
  memo1.lines.add('OpenHandle='+inttohex(longint(create),8)+#13);

  iConnectHandle:=InternetConnect(create,pchar('192.168.254.101'),80,pchar('sruser'),pchar('sruser'),INTERNET_SERVICE_HTTP,0,0);
  memo1.lines.add('ConnectHandle='+inttohex(longint(iconnecthandle),8)+#13);

  // create the file on the host
  requestHandle:=HttpOpenRequestA(iConNectHandle,pchar('PUT'),pchar('myFile.txt'),nil,pchar('myfile.txt'),nil,INTERNET_FLAG_NEED_FILE,0);
  memo1.lines.add('RequestHandle='+inttohex(longint(requestHandle),8)+#13);

  bytesWritten:=sizeof(buffer);
  while bytesWritten>=sizeof(buffer) do begin
    bytesWritten:=data.Read(buffer,sizeof(buffer));  //sizeof(buffer));
    iFileBuffer.dwBufferLength:=bytesWritten;
    iFileBuffer.dwBufferTotal:=bytesWritten;
    if HttpSendRequestEx(requestHandle,@iFileBuffer,nil,0,0) then memo1.lines.add('SendRequest Ok'+#13);
    memo1.lines.add('Write Size='+inttostr(bytesWritten));
  end;

  InternetCloseHandle(requestHandle);
  InternetCloseHandle(iConnectHandle);
  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;
Avatar of mgazza

ASKER

that looks gud ill get back 2 u as soon as i have time to check it :D
Avatar of mgazza

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
Avatar of mgazza

ASKER

ok the internet connect loged in ok (connect <>nil and request <>nil) humm how about the request is put a real value?

HttpOpenRequestA(iConNectHandle,pchar('PUT'),pchar('myFile.txt'),nil,pchar('myfile.txt'),nil,INTERNET_FLAG_NEED_FILE,0);

do i have to put a url infont of the file names? and why 2 file names?
Avatar of mgazza

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.
Avatar of mgazza

ASKER

so it would be

HttpOpenRequestA(iConNectHandle,pchar('PUT'),pchar('http://www.mgazzsoft.150m.com/myFile.txt'),nil,pchar('myfile.txt'),nil,INTERNET_FLAG_NEED_FILE,0);

? please change this if it is wrong
Avatar of mgazza

ASKER

cant i use the ftp open file?
I think you shout put the following

 requestHandle:=HttpOpenRequestA(iConNectHandle,pchar('PUT'),
  pchar('myFile.txt'),nil,nil,nil,INTERNET_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
Avatar of mgazza

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
Avatar of mgazza

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
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.

ASKER CERTIFIED SOLUTION
Avatar of JDuncan
JDuncan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mgazza

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?
Avatar of mgazza

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.....!)