Jump to content

Unigui Calling Web Services or API


soon

Recommended Posts

I don't have an example right now, but a web service is basically just an HTTP server,

so use any HTTP client and make a call, and read the result, XML or JSON....parse it,

push it into a table and refresh the query linked to your grid, and there you have it.

 

I like using the Indy components, the HTTP client is blocking, so use a thread if you

have to, but it is very easy to set up and there are tons of examples on the net.

Link to comment
Share on other sites

I don't have an example right now, but a web service is basically just an HTTP server,

so use any HTTP client and make a call, and read the result, XML or JSON....parse it,

push it into a table and refresh the query linked to your grid, and there you have it.

 

I like using the Indy components, the HTTP client is blocking, so use a thread if you

have to, but it is very easy to set up and there are tons of examples on the net.

Do you mean TIdHTTP ? :)

Link to comment
Share on other sites

@soon: yes, the indy http client.

 

Below is a function I previously used to send SMS via a phone operator web service, using http.get and no ssl:

 

function TuniMainModule.SendSMS(number, msg:string):string;
const
  cUSER_AGENT = 'Mozilla/4.0 (MSIE 6.0; Windows NT 5.1)';
var
 httpResponse: string;
 Stream: TStringStream;
 URL, telParams:string;
 HTTP:TidHTTP;
begin
  Stream := TStringStream.Create;
  HTTP:=TIdHTTP.Create(nil);
  HTTP.ReadTimeout := 10000;{ IdTimeoutInfinite; }
  HTTP.ConnectTimeout := 10000;
  try
    HTTP.Request.UserAgent := cUSER_AGENT;
    try
      HTTP.Get(URL, Stream);
      result:= Stream.DataString;
    except
      result:= 'SMS Error!';
    end;
  finally
    Stream.Free;
    HTTP.Free;
  end;
end;
 
If you want basic http auth then add:
 
HTTP.Request.BasicAuthentication:=true;
HTTP.Request.Username:=username;
HTTP.Request.Password:=pw;
 
If you need SSL, add:
 
var
LHandler: TIdSSLIOHandlerSocketOpenSSL;
...
try
LHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);

...

HTTP.IOHandler:=LHandler;

finally

LHandler.Free;

 

etc.

 

If you need to POST, use HTTP.Post(url, aParams, aResponse);

 

Notice: using GET you have to think about encoding, since this is

part of the URL, but when using POST there is no worry about that.

Link to comment
Share on other sites

@soon: yes, the indy http client.

 

Below is a function I previously used to send SMS via a phone operator web service, using http.get and no ssl:

 

function TuniMainModule.SendSMS(number, msg:string):string;
const
  cUSER_AGENT = 'Mozilla/4.0 (MSIE 6.0; Windows NT 5.1)';
var
 httpResponse: string;
 Stream: TStringStream;
 URL, telParams:string;
 HTTP:TidHTTP;
begin
  Stream := TStringStream.Create;
  HTTP:=TIdHTTP.Create(nil);
  HTTP.ReadTimeout := 10000;{ IdTimeoutInfinite; }
  HTTP.ConnectTimeout := 10000;
  try
    HTTP.Request.UserAgent := cUSER_AGENT;
    try
      HTTP.Get(URL, Stream);
      result:= Stream.DataString;
    except
      result:= 'SMS Error!';
    end;
  finally
    Stream.Free;
    HTTP.Free;
  end;
end;
 
If you want basic http auth then add:
 
HTTP.Request.BasicAuthentication:=true;
HTTP.Request.Username:=username;
HTTP.Request.Password:=pw;
 
If you need SSL, add:
 
var
LHandler: TIdSSLIOHandlerSocketOpenSSL;
...
try
LHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);

...

HTTP.IOHandler:=LHandler;

finally

LHandler.Free;

 

etc.

 

If you need to POST, use HTTP.Post(url, aParams, aResponse);

 

Notice: using GET you have to think about encoding, since this is

part of the URL, but when using POST there is no worry about that.

ok, thank you so much for your help :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...