I need to POST a form over HTTPS to a server with an unvalidated SSL certificate. This code works over HTTP:
void PostForm () {
WWWForm form = new WWWForm();
form.AddField( "variable1", 123 );
WWW postRequest = new WWW( "http://192.168.1.2/post", form );
}
When I switch url to “https://…” , it doesn’t work because the SSL certificate is not trusted. My guess is I would need to allow all certificates, but I don’t know how.
I found this answer, which accepts all sertificates, and after pasting the code and including IO, Net, Net.Security, and Security.Cryptography imports I was able to receive a GET request through HTTPS. How do I POST a form with this method?
using UnityEngine;
using System.Collections;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class POST_Form : MonoBehaviour {
IEnumerator Start ()
{
ServicePointManager.ServerCertificateValidationCallback = TrustCertificate;
HttpWebRequest request = (HttpWebRequest) WebRequest.Create( "https://192.168.1.2/post" );
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
Stream dataStream = response.GetResponseStream ();
StreamReader reader = new StreamReader (dataStream);
string responseFromServer = reader.ReadToEnd ();
Debug.Log ("responseFromServer=" + responseFromServer );
yield return 0;
}
//http://stackoverflow.com/questions/3674692/mono-webclient-invalid-ssl-certificates
private static bool TrustCertificate(object sender, X509Certificate x509Certificate, X509Chain x509Chain, SslPolicyErrors sslPolicyErrors)
{
// all Certificates are accepted
return true;
}
}
I’m not skillful in HTTP code and have no idea what these functions do (no good with vocab, either). Please provide a complete script which I can test.
Hi guys I would need some help if you can, I tried to enter the lines that you have commented, but it remains the error on HTTPS… Do you have any suggestion? Thank you very very much
using UnityEngine;
using System.Collections;
using System.Collections.Generic; //Needed for Lists
using System.Xml; //Needed for XML functionality
using System.Xml.Serialization; //Needed for XML Functionality
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Xml.Linq; //Needed for XDocument