Delphi_852 Posted May 10, 2017 at 02:34 PM Report Share #604124 Posted May 10, 2017 at 02:34 PM Boa Tarde, Necessito fazer aplicação que faz pedido através da api Rest permita obter dados do servidor. Esta chamada utiliza autenticação através de um certificado digital... porém não estou a conseguir... obtenho o seguinte erro: "The request was aborted: Could not create SSL/TLS secure channel.".. Alguém me pode ajudar?? Segue parte do meu código.... Appi.config: <?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.net> <settings> <httpWebRequest useUnsafeHeaderParsing="true" /> </settings> <defaultProxy> <proxy bypassonlocal="True" usesystemdefault="False" /> </defaultProxy> </system.net> </configuration> public enum HttpVerb { GET, POST, PUT, DELETE } namespace HttpUtils { public class RestClient { public string EndPoint { get; set; } public HttpVerb Method { get; set; } public string ContentType { get; set; } public string PostData { get; set; } public RestClient() { EndPoint = ""; Method = HttpVerb.GET; ContentType = "text/xml"; PostData = ""; } public RestClient(string endpoint) { EndPoint = endpoint; Method = HttpVerb.GET; ContentType = "text/xml"; PostData = ""; } public RestClient(string endpoint, HttpVerb method) { EndPoint = endpoint; Method = method; ContentType = "text/xml"; PostData = ""; } public RestClient(string endpoint, HttpVerb method, string postData) { EndPoint = endpoint; Method = method; ContentType = "text/xml"; PostData = postData; } public string MakeRequest() { return MakeRequest(""); } public string MakeRequest(string parameters) { ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; var request = (HttpWebRequest)WebRequest.Create(EndPoint + parameters); string certFileName = Path.Combine("C:\\....", "cert.p12"); request.ProtocolVersion = HttpVersion.Version10; X509Certificate2 cert = new X509Certificate2(certFileName, "pass"); request.KeepAlive = false; request.ServicePoint.Expect100Continue = false; request.Method = Method.ToString(); request.ContentLength = 0; request.ContentType = ContentType; request.ClientCertificates.Add(cert); request.PreAuthenticate = true; if (!string.IsNullOrEmpty(PostData) && Method == HttpVerb.POST) { var encoding = new UTF8Encoding(); var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(PostData); request.ContentLength = bytes.Length; using (var writeStream = request.GetRequestStream()) { writeStream.Write(bytes, 0, bytes.Length); } } using (var response = (HttpWebResponse)request.GetResponse()) { var responseValue = string.Empty; if (response.StatusCode != HttpStatusCode.OK) { var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode); throw new ApplicationException(message); } // grab the response using (var responseStream = response.GetResponseStream()) { if (responseStream != null) using (var reader = new StreamReader(responseStream)) { responseValue = reader.ReadToEnd(); } } return responseValue; } } } // class } E a chamada: string endPoint = @"https://......./json"; var client = new RestClient(endPoint); var json = client.MakeRequest(); Obrigado pela atenção... Link to comment Share on other sites More sharing options...
Delphi_852 Posted May 11, 2017 at 07:10 AM Author Report Share #604137 Posted May 11, 2017 at 07:10 AM Ninguém me consegue ajudar? Link to comment Share on other sites More sharing options...
iron Posted May 11, 2017 at 12:26 PM Report Share #604140 Posted May 11, 2017 at 12:26 PM http://stackoverflow.com/questions/32994464/could-not-create-ssl-tls-secure-channel-despite-setting-servercertificatevalida http://stackoverflow.com/questions/2859790/the-request-was-aborted-could-not-create-ssl-tls-secure-channel vê se ajuda Cumprimentos, iron Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now