How to convert C# code calling a rest service to Delphi XE4 -
can me on how convert following c# code delphi xe4?
//libraries //custom parameters string url = "rest_service_url"; string sauthorization = "username:password"; string filename = "filename.zip"; string filepath = @"file_path" + filename; //identificate separator string boundary = "---------------------------" + datetime.now.ticks.tostring("x"); //encoding byte[] boundarybytes = system.text.encoding.ascii.getbytes("\r\n--" + boundary + "\r\n"); //creation , specification of request httpwebrequest wr = (httpwebrequest)webrequest.create(url); wr.contenttype = "multipart/form-data; boundary=" + boundary; wr.method = "post"; wr.keepalive = true; wr.credentials = system.net.credentialcache.defaultcredentials; //authentication byte[] toencodeasbytes = system.text.asciiencoding.ascii.getbytes(sauthorization); string returnvalue = system.convert.tobase64string(toencodeasbytes); wr.headers.add("authorization: basic " + returnvalue); //writting of file stream rs = wr.getrequeststream(); rs.write(boundarybytes, 0, boundarybytes.length); byte[] formitembytes = system.text.encoding.utf8.getbytes(filepath); rs.write(formitembytes, 0, formitembytes.length); rs.write(boundarybytes, 0, boundarybytes.length); string headertemplate = "content-disposition: form-data; name=\"{0}\";filename=\"{1}\"\r\ncontent-type: {2}\r\n\r\n"; string header = string.format(headertemplate, "file", filename, "application/octet-stream"); byte[] headerbytes = system.text.encoding.utf8.getbytes(header); rs.write(headerbytes, 0, headerbytes.length); filestream filestream = new filestream(filepath, filemode.open, fileaccess.read); byte[] buffer = new byte[4096]; int bytesread = 0; while ((bytesread = filestream.read(buffer, 0, buffer.length)) = 0) { rs.write(buffer, 0, bytesread); } filestream.close(); byte[] trailer = system.text.encoding.ascii.getbytes("\r\n--" + boundary + "--\r\n"); rs.write(trailer, 0, trailer.length); rs.close(); rs = null; try { //get response
delphi ships indy, has tidhttp component , tidmultipartformdatastream class, eg:
uses ..., idhttp, idmultipartformdata; var filename: string; filepath: string; http: tidhttp; postdata: tidmultipartformdatastream; begin filename := 'filename.zip'; filepath := 'file_path' + filename; postdata := tidmultipartformdatastream.create; try postdata.addfile('file', filepath, 'application/octet-stream'); http := tidhttp.create; try http.request.username := 'username'; http.request.password := 'password'; http.request.basicauthentication := true; http.post('rest_service_url', postdata); http.free; end; postdata.free; end; //process response needed end; tidhttp.post() can return server's response data either string:
var response: string; response := http.post('rest_service_url', postdata); or tstream:
var response: tmemorystream; response := tmemorystream.create; // or whatever tstream class want try http.post('rest_service_url', postdata, response); ... response.free; end;
Comments
Post a Comment