ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

UnityWebRequest,Pos请求需要解码

2021-07-31 10:01:42  阅读:240  来源: 互联网

标签:jsonStr listerner 解码 ctx System Pos unityWeb using UnityWebRequest


简易服务器代码:

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有