Jump to content
mofareh

any body can write this code by delphi?

Recommended Posts

the code is vb.net ... i want to write it by delphi

Sub Main(filename As String, MassegeTxt As String, mobileNo As String)
        ''''' send image as base64
        Dim WebRequest As HttpWebRequest
        Dim bytes As Byte() = IO.File.ReadAllBytes(filename)
        Dim file As String = Convert.ToBase64String(bytes)

        'Dim mof As String = HttpUtility.UrlEncode(file)

        Dim instance_id As String = "instance xxxxx"
        Dim token As String = "xxxxxxx"
        Dim mobile_number As String = mobileNo
       


         WebRequest = HttpWebRequest.Create("https://api.ultramsg.com/" + instance_id + "/messages/image")
        Dim postdata As String = "token=" + token + "&to=" + mobileNo + "&image=" & HttpUtility.UrlEncode(file) & "&caption=" + MassegeTxt
        Dim enc As UTF8Encoding = New System.Text.UTF8Encoding()
        Dim postdatabytes As Byte() = enc.GetBytes(postdata)

        WebRequest.Method = "POST"

        WebRequest.ContentType = "application/x-www-form-urlencoded"
        WebRequest.GetRequestStream().Write(postdatabytes)
        Dim ret As New System.IO.StreamReader(WebRequest.GetResponse().GetResponseStream())
          Console.WriteLine(ret.ReadToEnd())


    End Sub

Share this post


Link to post

What is stopping you from writing this in Delphi?  I don't fully understand the question?  Do you have any programmers on your team?  Why not ask one of them to do this?

Edited by David Heffernan

Share this post


Link to post
10 minutes ago, David Heffernan said:

What is stopping you from writing this in Delphi I don't fully understand the question?  Do you have any programmers on your team?  Why not ask one of them to do this?

i dont know the vb.net 

i need any devlober write code by vb.net and delphi

 

we dont have team 

we small busnse 

 

 

Edited by mofareh

Share this post


Link to post
3 minutes ago, David Heffernan said:

Don't you have any software developers?

I am delphi developer but i want helping

Help me or stop question  

Share this post


Link to post
14 minutes ago, mofareh said:

I am delphi developer .


In that case, the code is not that difficult, as a Delphi developer you have to be able to do this for 99,9% on your own.
 

So make your own code in Delphi, see if it works, if it doesn't, then post that Delphi code here for reviewing/tips/improvements.

 

Edited by mvanrijnen
  • Like 2

Share this post


Link to post
3 hours ago, mofareh said:

the code is vb.net ... i want to write it by delphi

If you are using Indy, the code would look something like this:

uses
  ..., System.Classes, IdCoderMIME, IdHTTP, System.IOUtils;

procedure Main(const filename, MassegeTxt, mobileNo: String);
var
  WebRequest: TIdHTTP;
  SSL: TIdSSLIOHandlerSocketOpenSSL;
  bytes: TBytes;
  file: String;
  instance_id: String;
  token: String;
  mobile_number: String;
  postdata: TStringList;
begin
  // send image as base64
  bytes := TFile.ReadAllBytes(filename);
  file := TIdEncoderMIME.EncodeBytes(bytes);

  instance_id := 'instance xxxxx';
  token := 'xxxxxxx';
  mobile_number := mobileNo;

  WebRequest := TIdHTTP.Create;
  try
    SSL := TIdSSLIOHandlerSocketOpenSSL.Create(WebRequest);
    SSL.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
    WebRequest.IOHandler := SSL;

    WebRequest.Request.ContentType := 'application/x-www-form-urlencoded';

    postdata := TStringList.Create;
    try
      postdata.Add('token=' + token);
      postdata.Add('to=' + mobileNo);
      postdata.Add('image=' + file);
      postdata.Add('caption=' + MassegeTxt);

      Writeln(WebRequest.Post('https://api.ultramsg.com/' + instance_id + '/messages/image', postdata));
    finally
      postdata.Free;
    end;
  finally
    WebRequest.Free;
  end;
end;

If you want to use Embarcadero's TNetHTTPClient, the code would look something like this instead:

uses
  ..., System.Classes, System.SysUtils, System.NetEncoding, System.Net.HTTPClientComponent, System.IOUtils;

procedure Main(const filename, MassegeTxt, mobileNo: String);
var
  WebClient: TNetHTTPClient;
  bytes: TBytes;
  file: String;
  instance_id: String;
  token: String;
  mobile_number: String;
  postdata: TStringList;
begin
  // send image as base64
  bytes := TFile.ReadAllBytes(filename);
  file := TNetEncoding.Base64.EncodeBytesToString(bytes);

  instance_id := 'instance xxxxx';
  token := 'xxxxxxx';
  mobile_number := mobileNo;

  WebClient := TNetHTTPClient.Create(nil);
  try
    WebClient.ContentType := 'application/x-www-form-urlencoded';

    postdata := TStringList.Create;
    try
      postdata.Add('token=' + token);
      postdata.Add('to=' + mobileNo);
      postdata.Add('image=' + file);
      postdata.Add('caption=' + MassegeTxt);

      Writeln(WebClient.Post('https://api.ultramsg.com/' + instance_id + '/messages/image', postdata, nil, TEncoding.UTF8, nil).ContentAsString());
    finally
      postdata.Free;
    end;
  finally
    WebClient.Free;
  end;
end;

Or, using Embarcadero's THTTPClient instead:

uses
  ..., System.Classes, System.SysUtils, System.IOUtils, System.NetEncoding, System.Net.HttpClient;

procedure Main(const filename, MassegeTxt, mobileNo: String);
var
  WebClient: THTTPClient;
  bytes: TBytes;
  file: String;
  instance_id: String;
  token: String;
  mobile_number: String;
  postdata: TStringList;
begin
  // send image as base64
  bytes := TFile.ReadAllBytes(filename);
  file := TNetEncoding.Base64.EncodeBytesToString(bytes);

  instance_id := 'instance xxxxx';
  token := 'xxxxxxx';
  mobile_number := mobileNo;

  WebClient := THTTPClient.Create;
  try
    WebClient.ContentType := 'application/x-www-form-urlencoded';

    postdata := TStringList.Create;
    try
      postdata.Add('token=' + token);
      postdata.Add('to=' + mobileNo);
      postdata.Add('image=' + file);
      postdata.Add('caption=' + MassegeTxt);

      Writeln(WebClient.Post('https://api.ultramsg.com/' + instance_id + '/messages/image', postdata, nil, TEncoding.UTF8, nil).ContentAsString());
    finally
      postdata.Free;
    end;
  finally
    WebClient.Free;
  end;
end;

 

  • Like 1

Share this post


Link to post
1 hour ago, Remy Lebeau said:

If you are using Indy, the code would look something like this:


uses
  ..., System.Classes, IdCoderMIME, IdHTTP, System.IOUtils;

procedure Main(const filename, MassegeTxt, mobileNo: String);
var
  WebRequest: TIdHTTP;
  SSL: TIdSSLIOHandlerSocketOpenSSL;
  bytes: TBytes;
  file: String;
  instance_id: String;
  token: String;
  mobile_number: String;
  postdata: TStringList;
begin
  // send image as base64
  bytes := TFile.ReadAllBytes(filename);
  file := TIdEncoderMIME.EncodeBytes(bytes);

  instance_id := 'instance xxxxx';
  token := 'xxxxxxx';
  mobile_number := mobileNo;

  WebRequest := TIdHTTP.Create;
  try
    SSL := TIdSSLIOHandlerSocketOpenSSL.Create(WebRequest);
    SSL.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
    WebRequest.IOHandler := SSL;

    WebRequest.Request.ContentType := 'application/x-www-form-urlencoded';

    postdata := TStringList.Create;
    try
      postdata.Add('token=' + token);
      postdata.Add('to=' + mobileNo);
      postdata.Add('image=' + file);
      postdata.Add('caption=' + MassegeTxt);

      Writeln(WebRequest.Post('https://api.ultramsg.com/' + instance_id + '/messages/image', postdata));
    finally
      postdata.Free;
    end;
  finally
    WebRequest.Free;
  end;
end;

If you want to use Embarcadero's TNetHTTPClient, the code would look something like this instead:


uses
  ..., System.Classes, System.SysUtils, System.NetEncoding, System.Net.HTTPClientComponent, System.IOUtils;

procedure Main(const filename, MassegeTxt, mobileNo: String);
var
  WebClient: TNetHTTPClient;
  bytes: TBytes;
  file: String;
  instance_id: String;
  token: String;
  mobile_number: String;
  postdata: TStringList;
begin
  // send image as base64
  bytes := TFile.ReadAllBytes(filename);
  file := TNetEncoding.Base64.EncodeBytesToString(bytes);

  instance_id := 'instance xxxxx';
  token := 'xxxxxxx';
  mobile_number := mobileNo;

  WebClient := TNetHTTPClient.Create(nil);
  try
    WebClient.ContentType := 'application/x-www-form-urlencoded';

    postdata := TStringList.Create;
    try
      postdata.Add('token=' + token);
      postdata.Add('to=' + mobileNo);
      postdata.Add('image=' + file);
      postdata.Add('caption=' + MassegeTxt);

      Writeln(WebClient.Post('https://api.ultramsg.com/' + instance_id + '/messages/image', postdata, nil, TEncoding.UTF8, nil).ContentAsString());
    finally
      postdata.Free;
    end;
  finally
    WebClient.Free;
  end;
end;

Or, using Embarcadero's THTTPClient instead:


uses
  ..., System.Classes, System.SysUtils, System.IOUtils, System.NetEncoding, System.Net.HttpClient;

procedure Main(const filename, MassegeTxt, mobileNo: String);
var
  WebClient: THTTPClient;
  bytes: TBytes;
  file: String;
  instance_id: String;
  token: String;
  mobile_number: String;
  postdata: TStringList;
begin
  // send image as base64
  bytes := TFile.ReadAllBytes(filename);
  file := TNetEncoding.Base64.EncodeBytesToString(bytes);

  instance_id := 'instance xxxxx';
  token := 'xxxxxxx';
  mobile_number := mobileNo;

  WebClient := THTTPClient.Create;
  try
    WebClient.ContentType := 'application/x-www-form-urlencoded';

    postdata := TStringList.Create;
    try
      postdata.Add('token=' + token);
      postdata.Add('to=' + mobileNo);
      postdata.Add('image=' + file);
      postdata.Add('caption=' + MassegeTxt);

      Writeln(WebClient.Post('https://api.ultramsg.com/' + instance_id + '/messages/image', postdata, nil, TEncoding.UTF8, nil).ContentAsString());
    finally
      postdata.Free;
    end;
  finally
    WebClient.Free;
  end;
end;

 

How can I thank you??
The code is post correctly
Thank you very much

Share this post


Link to post
13 hours ago, mofareh said:

How can I thank you??

Well, not quoting the entire post would be good start...

  • Like 1
  • Haha 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×