帖子

Unity环境下,C#demo改的,报错404,HttpUtility也有问题

[复制链接]

该用户从未签到

4591 147****5410 发表于 2019-5-6 14:29:45 1#
首先源代码
private void Start()
    {
        Headers();
    }

    public static String Md5(string s)
    {
        System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);
        bytes = md5.ComputeHash(bytes);
        md5.Clear();
        string ret = "";
        for (int i = 0; i < bytes.Length; i++)
        {
            ret += Convert.ToString(bytes, 16).PadLeft(2, '0');
        }
        return ret.PadLeft(32, '0');
    }

    public static void Headers()
    {
        string x_appid = "5cc5cceb1d0";
        string api_key = "e59e59adf7518755f773f82ce842bf8d258";
        string path = @"Assets\xunfei\AudioSource/cn_syll.wav";

        string param = "{\"aue\":\"raw\",\"result_level\":\"entirety\",\"language\":\"zh_cn\",\"category\":\"read_sentence\"}";

        System.Text.Encoding encode = System.Text.Encoding.ASCII;
        byte[] bytedata = encode.GetBytes(param);
        string x_param = Convert.ToBase64String(bytedata);

        TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
        string curTime = Convert.ToInt64(ts.TotalSeconds).ToString();

        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
        string result = string.Format("{0}{1}{2}", api_key, curTime, x_param);
        string x_checksum = SoundTest.Md5(result);

        byte[] arr = File.ReadAllBytes(path);
        string cc = Convert.ToBase64String(arr);
        string data = "audio=" + cc;

        data += "&text=" + HttpUtility.UrlEncode("中美数控");

        String Url = "http://api.xfyun.cn/v1/service/v1/ise";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
        request.Headers["X-Appid"] = "5cceb1d0";
        request.Headers["X-CurTime"] = curTime;  
        request.Headers["X-Param"] = x_param;
        request.Headers["X-Checksum"] = x_checksum;

        var postdata = Encoding.ASCII.GetBytes(data);
        request.ContentLength = data.Length;

        //text = WWW.EscapeURL(text, Encoding.UTF8)
        //data += "&text=" + HttpUtility.UrlEncode("中美数控");

        using (var stream = request.GetRequestStream())
        {
            stream.Write(postdata, 0, postdata.Length);
        }

        string htmlStr = string.Empty;
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        Stream responseStream = response.GetResponseStream();
        using (StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8")))
        {
            htmlStr = reader.ReadToEnd();
        }
        responseStream.Close();
        print(htmlStr);
    }


直接用C#代码放入Unity里改参数修改的,然后其中这里报错 图片1.png

后来将那句报错代码注释掉了,运行后报错404 图片2.png
第一次尝试,有点不知道为什么

讯飞2017hj 发表于 2019-5-7 18:05:55
2#
好像没链接上,是跨域访问的吗?
使用道具 举报 回复