POST a form over HTTPS with unvalidated SSL Certificate

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.

Thank you.

something like:

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://yoururl.com");
    request.ContentType = "application/x-www-form-urlencoded";
    request.Method = "POST";

    NameValueCollection nvc = new NameValueCollection();
    nvc.Add("PlayerName", "TommyTwoKills");
    nvc.Add("pId", "203984089sdlkfj-sdf9");

    StringBuilder postVars = new StringBuilder();
    foreach(string key in nvc)
        postVars.AppendFormat("{0}={1}&", key, nvc[key]);

    postVars.Length -= 1; // clip off the remaining &

    using (var streamWriter = new StreamWriter(request.GetRequestStream()))
        streamWriter.Write(postVars.ToString());

Followed by your GetRequesStream business.

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

public class Networking : MonoBehaviour
{
private string filepath = “mywebsite.com is available for purchase - Sedo.com”;

public void Read()
{
    
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.mywebsite.com/service/xml/testfile.xml");
    request.ContentType = "application/x-www-form-urlencoded";
    request.Method = "POST";
    XDocument doc = XDocument.Load(filepath);

    foreach (XElement el in doc.Root.Elements())
    {
        Debug.Log(string.Format("{0} {1}", el.Name, el.Attribute("id").Value));
        Debug.Log(string.Format("  Attributes:"));
        foreach (XAttribute attr in el.Attributes())
            Debug.Log(string.Format("    {0}", attr));
        Debug.Log(string.Format("  Elements:"));

        foreach (XElement element in el.Elements())
            Debug.Log(string.Format("    {0}: {1}", element.Name, element.Value));
    }
}

private static bool TrustCertificate(object sender, X509Certificate x509Certificate, X509Chain x509Chain, SslPolicyErrors sslPolicyErrors)
{
    // all Certificates are accepted
    return true;

}

}