Jump to content

mazluta

Members
  • Content Count

    18
  • Joined

  • Last visited

Community Reputation

0 Neutral
  1. Thanks Remy, I Will try the binary data and the JSON way.
  2. Hi Remy. First: thanks for the knowledge. Secondly: the "DevApp" field was sent as C# from the start. Third: the line - content = Uri.EscapeDataString(content); fix it all put "%2B" as replaced for any "+" sign and the Delphi Rest got the '+' sign as it should be without doing anything in the service. Fourth: when i sent the Base64 string back with '+' sign in the response the C# read it well and saved the PDF to wherever it should be saved.
  3. Hi Hi Remy Lebeau if the Solution is to do that in C# : https://stackoverflow.com/questions/7842547/request-parameter-losing-plus-sign descriptionsUrlAddition = descriptionsUrlAddition.replace("+", "%2B"); Then my Solution is the same i tried : request.ContentType = "application/form-data"; request.ContentType = "application/raw"; some give there solution like content = Uri.EscapeDataString(content); and that work fine.
  4. Hi Remy Lebeau i think your answer is the right answer. this is the C# Code private void button1_Click(object sender, EventArgs e) { string requestUrl = "http://185.185.135.XXX:YYYY/MyTest"; HttpWebRequest request = HttpWebRequest.CreateHttp(requestUrl); request.Method = "POST"; // Optionally, set properties of the HttpWebRequest, such as: request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; request.ContentType = "application/x-www-form-urlencoded"; // Could also set other HTTP headers such as Request.UserAgent, Request.Referer, // Request.Accept, or other headers via the Request.Headers collection. //Byte[] fileBytes = File.ReadAllBytes(@textBox1.Text, System.Text.Encoding.ASCII); Byte[] fileBytes = File.ReadAllBytes(@textBox1.Text); var content = Convert.ToBase64String(fileBytes,Base64FormattingOptions.None); // Set the POST request body data. In this example, the POST data is in // application/x-www-form-urlencoded format. string postData = "DevApp=C#&Base64Type=base64&username=mazluta&base64=" + content; using (var writer = new StreamWriter(request.GetRequestStream())) { writer.Write(postData); } // Submit the request, and get the response body from the remote server. string responseFromRemoteServer; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { responseFromRemoteServer = reader.ReadToEnd(); fileBytes = Convert.FromBase64String(responseFromRemoteServer); File.WriteAllBytes(@"c:\a\mytest.pdf", fileBytes); MessageBox.Show("File c:\\a\\mytest.pdf signed and saved"); } } } This is The Delphi Rest Code : procedure TMyWebModule.MyWebModuleactSignAction(Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean); Var RequestObject: TJSONObject; PdfBase64 : string; RspBase64 : string; UserName : string; Base64Type : string; DevApp : string; CachPdfPath : String; CachFilesPath : String; TmpPdfFile : String; TmpTxtFile : String; DstPdfFileName : String; SignParam : TSignParam; PfxFileName : String; PfxPassword : String; UserDataRec : TUserDataRec; ParamArea : TParamArea; UserSignData : TUserSignData; //aPdfBase64 : AnsiString; aRspBase64 : PAnsiChar; aTmpPdfFile : AnsiString; aTmpTxtFile : AnsiString; SaveTextList : TStringList; MyBytesArr : TBytes; CurChar : Integer; begin ( JustWriteToLog(' '); JustWriteToLog('======'); JustWriteToLog('reqeust=base64 at : ' + formatdatetime('dd/mm/yyyy hh:nn:ss:zzz', now)); JustWriteToLog(' '); UserName := Request.ContentFields.Values['UserName']; Base64Type := Request.ContentFields.Values['Base64Type']; PdfBase64 := Request.ContentFields.Values['Base64']; DevApp := Request.ContentFields.Values['DevApp']; JustWriteToLog(' '); JustWriteToLog('UserName='+UserName); JustWriteToLog(' '); JustWriteToLog('Base64Type='+Base64Type); JustWriteToLog(' '); JustWriteToLog('DevApp='+DevApp); JustWriteToLog(' '); JustWriteToLog('Base64='+PdfBase64); JustWriteToLog(' '); UserDataRec := dm_DB.GetUserData(UserName); ParamArea := dm_DB.LoadAppParams; JustWriteToLog('User request = ' + UserName); JustWriteToLog('Start PDF 50 char = ' + Copy(PdfBase64, 1, 50)); if UserDataRec.Found then JustWriteToLog('User Found') else JustWriteToLog('User not found'); if ParamArea.Found then JustWriteToLog('ParamArea Record found'); If not ParamArea.Found Then begin Response.Content := 'Fail load Server params'; Response.StatusCode := 400; exit; end; UserSignData := dm_DB.GetUserSignRec(UserDataRec, ParamArea); If not UserSignData.Found Then begin Response.Content := 'Fail load UserSignData params'; Response.StatusCode := 400; exit; end; JustWriteToLog('Find User SignData, PfxFile = ' + UserSignData.User_PfxFileName); JustWriteToLog('Find User SignData, LogoFile = ' + UserSignData.User_LogoFileName); CachPdfPath := GetBaseAppPath + '\' + DSHTTPWebDispatcher1.CacheContext; if CachPdfPath[length(CachPdfPath)] = '/' Then CachPdfPath[length(CachPdfPath)] := ' '; CachPdfPath := Trim(CachPdfPath); ForceDirectories(CachPdfPath); Try TmpPdfFile := CachPdfPath + '\Tmp_' + GetRandomStr + '.pdf'; TmpTxtFile := ChangeFileExt(TmpPdfFile,'.txt'); if UpperCase(DevApp) = UpperCase('C#') then begin JustWriteToLog('Before Replace PdfBase64 : ' + PdfBase64); SetLength(MyBytesArr,Length(PdfBase64)); MyBytesArr := TEncoding.ASCII.GetBytes(PdfBase64); for CurChar := 1 to Length(PdfBase64) do begin if Ord(MyBytesArr[CurChar]) = 32 then MyBytesArr[CurChar] := byte(43); end; PdfBase64 := TEncoding.ASCII.GetString(MyBytesArr); JustWriteToLog('After Replace PdfBase64 : ' + PdfBase64); ConvertBase64ToPdfFile(PdfBase64,TmpPdfFile); end else if UpperCase(Base64Type) = UpperCase('Base64') then begin ConvertBase64ToPdfFile(PdfBase64,TmpPdfFile); end else begin ConvertBase64MimeToPdfFile(PdfBase64,TmpPdfFile); end; JustWriteToLog('PDF File : ' + TmpPdfFile + ' Saved'); Except on e: exception do begin JustWriteToLog('Error on Save PDF File : ' + e.Message); Response.StatusCode := 390; Exit; end; End; DstPdfFileName := CachPdfPath + '\Rslt_' + GetRandomStr + '.pdf'; CachFilesPath := GetBaseAppPath + '\Files'; so - it looks that when I read the value of the Base64 field - the + becomes space PdfBase64 := Request.ContentFields.Values['Base64'];
  5. Hi, I use this DLL and it give good support and good documentation + examples
  6. Hi mvanrijnen i dont think it's the CRLF. The + sign is replace with the Space sign (43 --> 32)
  7. Hi Remy I Could not figure out how to Send binary data from C# to Delphi REST?
  8. Ok, it looks like I figured it out. but I am not sure it works for every c# version 🙂 (can't trust Windows developer). The Base64FormattingOptions_None.txt file contains the text of base64 created in C# before sending it to Delphi Rest. The ThisWhatDelphiRestGet.txt file contains the base64 text that Delphi rest moves to my var after Http Request envoke. The LogBase64Different.txt contains the differences Including Char No# and The Char ItSelf. It looks like DELPHI REST replaced the ASCII 43 to 32 or the HTTP request of the C# sent ASCII 32 instead of ASCII 43. So, If The Request comes from C#, I just convert all appearances of ASCII char 32 to 43, then save The PDF, and all work. SetLength(MyBytesArr,Length(PdfBase64)); MyBytesArr := TEncoding.ASCII.GetBytes(PdfBase64); for CurChar := 1 to Length(PdfBase64) do begin if Ord(MyBytesArr[CurChar]) = 32 then MyBytesArr[CurChar] := byte(43); end; PdfBase64 := TEncoding.ASCII.GetString(MyBytesArr); Can I Trust That Solution? Or I Missed Something? Thanks, Yossi Base64FormattingOptions_None.txt ThisWhatDelphiRestGet.txt LogBase64Different.txt
  9. He remy. 1. I will prepare a text file that holds what c# sends and what Delphi Rest reads. 2. how do I send PDF as binary data from C# to Delphi Rest? 3. dose it matter if the C# is AnyCPU or X64 and the rest is x86? Thanks, Yossi
  10. I write RESTserver with Delphi 10.3 The Service accepts 3 parameters. One of them is the string hold base64 string of PDF file. When I create the Base64 String with Delphi as : function ConvertPdfFileToBase64D(PdfFileName : String; var Base64Str : String) : Boolean; var success : Boolean; b64 : String; fBytes : TBytes; fSize : Integer; function FileToBytes(const AFileName: string; var Bytes: TBytes): Boolean; var Stream: TFileStream; begin if not FileExists(AFileName) then begin Result := False; Exit; end; Stream := TFileStream.Create(AFileName, fmOpenRead); try fSize := Stream.Size; SetLength(Bytes, fSize); Stream.ReadBuffer(Pointer(Bytes)^, fSize); finally Stream.Free; end; Result := True; end; begin Result := False; Base64Str := ''; if FileToBytes(PdfFileName,fBytes) then begin //Base64Str := TNetEncoding.Base64.EncodeBytesToString(fBytes, fSize); Base64Str := TNetEncoding.Base64.EncodeBytesToString(fBytes); Result := True; End; end; when i get this base64 in the Delphi REST Api I decode the string and successfully save a PDF file. when i send base64 string created in Visual Studio C# like : Byte[] fileBytes = File.ReadAllBytes(@textBox1.Text); var content = Convert.ToBase64String(fileBytes); I get different base64 strings. what is the right way to send base64 string as PDF file from C# to Delphi REST API
  11. well programmer..2k. that work. thanls b.t.w you are not so selfishness
  12. thanks. i will try it
  13. ".....customization can be done on events before send to client, or using a frameworks (3rd) like done by Intraweb, UniGui, etc... ....." can i write Web Service with UNIGUI ?
  14. I just want the Login form to look like this :
  15. first, thanks. I use my rest to get a PDF file as base64, a digital sign with some digital certificate. based on the user's table I want to popup him with the login form. but I don't want him to see the "Server Function.." label.
×