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;
LVL 1
mgazzaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JDuncanCommented:
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.
mgazzaAuthor Commented:
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
mgazzaAuthor Commented:
what is the procedure for connection to my server and wring some data to a file????
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

JDuncanCommented:
For http uploads the functions you have to call are

InternetOpen
InternetConnect
HTTPOpenRequest
InternetWriteFile
HTTPCloseRequest

JDuncanCommented:
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;
mgazzaAuthor Commented:
that looks gud ill get back 2 u as soon as i have time to check it :D
mgazzaAuthor Commented:
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
mgazzaAuthor Commented:
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?
mgazzaAuthor Commented:
requestex gets failed
JDuncanCommented:
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.
mgazzaAuthor Commented:
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
mgazzaAuthor Commented:
cant i use the ftp open file?
JDuncanCommented:
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
mgazzaAuthor Commented:
the url conponent isnt even used any ware and im still not writting a file :s
JDuncanCommented:
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
mgazzaAuthor Commented:
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
JDuncanCommented:
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.

JDuncanCommented:

This is the function I have now tested working on a live server.

Your ip address is 209.163.234.174 , you can get it by 'ping www.testspace.150.com' from cmd prompt
I can't get upload working to your server , I think the root of the server is at www.514.com and you need to know the folder virtual directory of the server where the file is going.

The root folder is probably set to read only and your www.testspace.com is pointing to a sub folder which you can write to with a password.

Your call should be something like this
HTTPUpload('209.163.234.174','/mydirectory/myfile.txt','c:\myfile.txt','TestSpace.150m.com','445566');

procedure TMainForm.HTTPUpload(Server,UplFile,locFile,user,pass:string);
var
  iFileBuffer,iRetBuffer : INTERNET_BUFFERS;
  requestHandle,iConnectHandle,create : PHandle;
  totalBytes,byteswritten : integer;
  buffer : array[0..512] of char;
  smem:tfilestream;
begin
  smem:=tfilestream.Create(locfile,fmOpenRead);

  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:=512;
  iFileBuffer.dwBufferTotal:=smem.size;
  iFileBuffer.dwOffsetLow:=0;
  iFileBuffer.dwOffsetHigh:=0;
   // connect to the internet
   create := InternetOpen('Mozilla/4.0 (compatible)', INTERNET_OPEN_TYPE_PRECONFIG , NIL, NIL, 0);
   memo1.lines.add('OpenHandle='+inttohex(longint(create),8)+#13);
   // connect to our server
   iConnectHandle:=InternetConnect(create,pchar(server),80,pchar(user),pchar(pass),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(uplfile),nil,nil,nil,INTERNET_FLAG_NEED_FILE,0);
   memo1.lines.add('RequestHandle='+inttohex(longint(requestHandle),8)+#13);
   totalBytes:=0;
   if word(requestHandle)>0 then begin
     bytesWritten:=sizeof(buffer);
     while bytesWritten>=sizeof(buffer) do begin
       bytesWritten:=smem.Read(buffer,sizeof(buffer));  //sizeof(buffer));
       iFileBuffer.dwBufferLength:=bytesWritten;
       iFileBuffer.dwBufferTotal:=bytesWritten;
       if not HttpSendRequestEx(requestHandle,@iFileBuffer,nil,0,0) then
         exit;
       totalBytes:=totalBytes+bytesWritten;
       bytes.text:=inttostr(totalBytes);
     end;
   end;
  InternetCloseHandle(requestHandle);
  InternetCloseHandle(iConnectHandle);
  InternetCloseHandle(create);
  smem.free;
end;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mgazzaAuthor Commented:
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?
mgazzaAuthor Commented:
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.....!)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.