using RestSharp; public class RestCaller { /// /// Classe gestione chiamate REST /// /// /// public RestCaller(string ApiUrl, int TimeOutSec) { tOut = TimeOutSec; apiUrl = ApiUrl; restOptStd = new RestClientOptions { Timeout = TimeSpan.FromSeconds(tOut), BaseUrl = new Uri(apiUrl) }; } /// /// Esecuzione chiamata Rest tipo GET /// /// /// public string ExecuteCallGet(string resource) { string answ = ""; if (!string.IsNullOrEmpty(resource)) { // client chiamate rest using (var client = new RestClient(restOptStd)) { var currReq = new RestRequest(resource, Method.Get); // currReq.AddHeader("Content-type", "application/xml"); currReq.AddHeader("Content-type", "application/json"); // effettuo vera chiamata var currResp = client.Get(currReq); if (currResp.StatusCode == System.Net.HttpStatusCode.OK && currResp.Content != null) { answ = $"{currResp.Content}"; } } } return answ; } protected string apiUrl = ""; protected int tOut = 60; /// /// Conf client RestSharp standard: /// - timeout 1 min /// private RestClientOptions restOptStd = new RestClientOptions { Timeout = TimeSpan.FromSeconds(60) }; }