- Home /
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.
Answer by Landern · Aug 09, 2016 at 05:26 PM
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.
Thank you, I finally figured it out. Here's the complete script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using UnityStandardAssets.CrossPlatformInput;
public class POST_Form : $$anonymous$$onoBehaviour {
void Update() {
if (CrossPlatformInput$$anonymous$$anager.GetButtonDown("Connect")){
PostForm ("https://192.168.1.2/post");
}
}
void PostForm (string url) {
ServicePoint$$anonymous$$anager.ServerCertificateValidationCallback = TrustCertificate;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.$$anonymous$$ethod = "POST";
NameValueCollection nvc = new NameValueCollection();
nvc.Add("variable1", "123");
nvc.Add("variable2", "this");
StringBuilder postVars = new StringBuilder();
foreach(string key in nvc)
postVars.AppendFormat("{0}={1}&", key, nvc[key]);
postVars.Length -= 1; // clip off the remaining &
//This
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
streamWriter.Write(postVars.ToString());
//Or this works
/*var streamWriter = new StreamWriter (request.GetRequestStream ());
streamWriter.Write (postVars.ToString());
streamWriter.Close();*/
}
private static bool TrustCertificate(object sender, X509Certificate x509Certificate, X509Chain x509Chain, SslPolicyErrors sslPolicyErrors) {
// all Certificates are accepted
return true;
}
}
But how to get response from the https url??
I haven't touched on this topic for a year now and don't remember anything. Take a look at my completed project's files with "HTTPS" in the name, maybe you'll find what you're looking for =)
Edit: looks like I haven't figured that out either...
Answer by mustang4484 · Nov 12, 2018 at 08:14 PM
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 = "https://www.mywebsite.com/service/xml/testfile.xml";
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;
}
}
Yeah, I'm definitely not the one to ask. Idk, maybe you can wait around for someone more knowledgeable to stumble upon your question, but I would recommend making a separate thread that will go on top of the recent questions list.
Your answer
Follow this Question
Related Questions
WWW/WWWForm, does Unity validate SSL certificates? 1 Answer
WWW/WWWForm, does Unity validate SSL certificates over HTTPS? 0 Answers
WWW and SSL on Android 1 Answer
Cannot get data from HTTP Response using UnityWebRequest in Unity 1 Answer
WWW with https resulting in SSL: couldn't create a context 0 Answers