• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by DemSec · Aug 09, 2016 at 05:18 AM · c#wwwwwwformhttpsssl

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DemSec · Aug 10, 2016 at 07:20 PM 0
Share

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;
     }
 }
avatar image sandeepsmartest DemSec · Jun 20, 2017 at 12:33 PM 0
Share

But how to get response from the https url??

avatar image DemSec sandeepsmartest · Jun 22, 2017 at 12:21 AM 0
Share

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 =)

https://github.com/Quantum-Helix/RaspberryPi_Video_Car/tree/master/Raspberry%20Pi%20Controller/Assets/Scripts

Edit: looks like I haven't figured that out either...

Show more comments
avatar image
0

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;


 }

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DemSec · Nov 12, 2018 at 08:18 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

WWW/WWWForm, does Unity validate SSL certificates? 1 Answer

WWW/WWWForm, does Unity validate SSL certificates over HTTPS? 0 Answers

Connect to HTTPS web service 0 Answers

Can't complete http POST request using webplayer build settings even after changing WWW security emulation 1 Answer

How to validate SSL certificate before sending data via https? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges