简体   繁体   中英

Using HTTP post to send Text to browser textarea using Unity3D and C#

I wonder if anyone could help me figure out why I can't get my text-to-speech app to work. I'm creating a desktop application where you type into an input area, hit enter, and your text appears in the output box. I want this text to also be sent to https://www.ivona.com so that it can be text-to-speech enabled.
I'm trying to use HTTP POST request to do so, but when I hit enter, the text only appears in my Unity project, and I get the error

"WebException : The remote server returned an error: (405) Method Not Allowed. System.Net.HttpWebRequest.CheckFinalStatus(System.Net.WebAsyncResult result).

My code is as follows :

using UnityEngine;
using System.Collections.Specialized;
using System.Collections.Generic;
using UnityEngine.UI; 
using System.Net;
using System.Linq;
using System.Text; 
using System.IO;
using System.Net.Security; 
using System.Security.Cryptography.X509Certificates; 
using System; 



public class Draft : MonoBehaviour {

InputField input; 
public Text output; 
InputField.SubmitEvent SubEv; 


// Use this for initialization
void Start () 
{
    input = gameObject.GetComponent<InputField> (); 
    SubEv = new InputField.SubmitEvent (); 
    SubEv.AddListener (SubmitInput); 
    input.onEndEdit = SubEv; 
}


private void SubmitInput (string arg0) 
{
    string currentText = output.text; 
    string newText = currentText + "\n" + arg0; 

    output.text = newText; 
    input.text = ""; 

    input.ActivateInputField (); 

    System.Net.ServicePointManager.ServerCertificateValidationCallback =
        new System.Net.Security.RemoteCertificateValidationCallback(delegate { return true; });

    string url = "https://www.ivona.com"; 
    string postData = output.text; 
    byte[] data = Encoding.UTF8.GetBytes (postData);
    var request = System.Net.WebRequest.Create (url) as System.Net.HttpWebRequest; 
    request.Method = "POST";
    request.ContentType = "application/json";

    request.ContentLength = data.Length; 
        ///string responseJson = null; 
    using (var requestStream = request.GetRequestStream ()) {
        requestStream.Write (data, 0, data.Length);
        requestStream.Close ();
    }

    using (var response = request.GetResponse () as System.Net.HttpWebResponse) {
        using (var reader = new System.IO.StreamReader (response.GetResponseStream ()))
            ;
            //responseJson = reader.ReadToEnd ();

    }

}

public bool MyRemoteCertificateValidationCallback (System.Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    bool isOK = true; 

    if (sslPolicyErrors != SslPolicyErrors.None) {

        for ( int i = 0; i < chain.ChainStatus.Length; i++) 
        {
            if (chain.ChainStatus [i].Status != X509ChainStatusFlags.RevocationStatusUnknown) 
            {
                chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain;
                chain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
                chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan (0, 1, 0); 
                chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
                bool chainIsValid = chain.Build ((X509Certificate2)certificate);

                if (!chainIsValid) {
                    isOK = false; 
                }

            }
        }
    }

    return isOK; 
}



public class MyPolicy : ICertificatePolicy 
{
    public bool CheckValidationResult (ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
    {
        return true;
    }
}



}           

If anyone could help me figure out why it's not posting to ivona what I type into Unity, I'd be very grateful.

Have a good day :)

Audaray

I tried the postcatcher and it seems that IVONA will only allow me to make such requests if I have the paid subscription like Paul-Jan says. I thought about it at the weekend and it seems that this is the case. Oh well - all part of the learning process :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM