其他分享
首页 > 其他分享> > UnityWebRequest,Pos请求需要解码

UnityWebRequest,Pos请求需要解码

作者:互联网

简易服务器代码:

using System;

namespace HttpServer
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    using System.Net;

    class Program
    {
        //服务器
        static void Main(string[] args)
        {
            using (HttpListener listerner = new HttpListener())
            {
                listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;//指定身份验证 Anonymous匿名访问
                listerner.Prefixes.Add("http://localhost:8080/web/");
                listerner.Start();
                Console.WriteLine("WebServer Start Successed.......");
                while (true)
                {
                    //等待请求连接
                    //没有请求则GetContext处于阻塞状态
                    HttpListenerContext ctx = listerner.GetContext();
                    ctx.Response.StatusCode = 200;//设置返回给客服端http状态代码
                    Console.WriteLine(ctx.Request.QueryString);
                    Stream stream = ctx.Request.InputStream;
                    //获取响应内容
                    StreamReader reader = new StreamReader(stream, Encoding.UTF8);
                    var result = reader.ReadToEnd();
                    Console.WriteLine(result);
                    var  str =   System.Web.HttpUtility.UrlDecode(result);
                    //Resposed  1.设置状态码
                    ctx.Response.StatusCode = 200;//200成功   其他是失败 可以自定义,在客户端对应解析即可
                    ctx.Response.ContentType = "application/json";//声明出参类型是json
                    using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream))
                    {
                        writer.WriteLine("Server");
                        writer.Close();
                        ctx.Response.Close();
                    }
                }
                listerner.Stop();
            }
        }
    }
}

输入内容进行了转码

 用此方法解码,得到正确的字符串

 客户端代码:

   public void PostUrl(LogData _data)
    {
        LoginData login = new LoginData();
        jsonStr = JsonUtility.ToJson(login, false);
        bytes = Encoding.UTF8.GetBytes(jsonStr);
        Debug.Log(jsonStr);
        unityWeb = UnityWebRequest.Post("http://localhost:8080/web/", jsonStr);
        unityWeb.useHttpContinue = false;
        StartCoroutine(Post(Url, _data));
    }
    private void LateUpdate()
    {
        if (Input.GetKeyDown(KeyCode.S))
        {
            PostUrl(null);
        }
    }

    UnityWebRequest unityWeb;
    string jsonStr;
    byte[] bytes;
    IEnumerator Post(string _url, LogData _data)
    {
        //客户端 
        unityWeb.SendWebRequest();
        yield return unityWeb;
        Debug.Log(unityWeb.url);
        if (unityWeb.isDone && !unityWeb.isHttpError)
        {
            Debug.Log("Success :" + unityWeb.downloadHandler.text);
        }
        else
        {
            Debug.LogError(unityWeb.error);
        }
        //WWW ww = new WWW("http://localhost:8080/web/", bytes);
        //yield return ww;
        //if (ww.isDone)
        //{
        //    Debug.LogError(ww.text);
        //}
    }

测试老API WWW,不会进行转码 -如下

 

标签:jsonStr,listerner,解码,ctx,System,Pos,unityWeb,using,UnityWebRequest
来源: https://blog.csdn.net/LM514104/article/details/119269495