I’m trying to communicate with my REST API through HttpWebRequest/Response. I managed to import the CAs from mozroots.exe, but I’m still getting WebExceptions/TlsExceptions.
Here is some example code that tries to use Flickr API test:
using System.IO;
using System.Net;
using UnityEngine;
public class TestScript : MonoBehaviour
{
void Start()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.flickr.com/services/rest/?method=flickr.test.echo&name=12345");
request.Method = "POST";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
Debug.Log(string.Format("Code: {0}
Desc: {1}
Type: {2}
Length: {3}
Encoding: {4}
Content: {5}", response.StatusCode, response.StatusDescription, response.ContentType, response.ContentLength, response.ContentEncoding, reader.ReadToEnd()));
reader.Close();
response.Close();
}
}
And here is the error:
TlsException: Invalid certificate received from server. Error code: 0xffffffff80092012
Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.validateCertificates (Mono.Security.X509.X509CertificateCollection certificates)
Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.ProcessAsTls1 ()
Mono.Security.Protocol.Tls.Handshake.HandshakeMessage.Process ()
(wrapper remoting-invoke-with-check) Mono.Security.Protocol.Tls.Handshake.HandshakeMessage:Process ()
Mono.Security.Protocol.Tls.ClientRecordProtocol.ProcessHandshakeMessage (Mono.Security.Protocol.Tls.TlsStream handMsg)
Mono.Security.Protocol.Tls.RecordProtocol.InternalReceiveRecordCallback (IAsyncResult asyncResult)
Rethrow as IOException: The authentication or decryption has failed.
Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (IAsyncResult asyncResult)
Rethrow as WebException: Error getting response stream (Write: The authentication or decryption has failed.): SendFailure
System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult)
System.Net.HttpWebRequest.GetResponse ()
TestScript.Start () (at Assets/TestScript.cs:11)
Does anyone know what I’m doing wrong?