201710152055->关于unity提交中文正文解析错误

来源:互联网 发布:中国帝国主义 知乎 编辑:程序博客网 时间:2024/05/30 22:59

分析:

之前利用www类中wwwform提交正文

经过服务器返回正文中中文的显示变乱码

查资料发现wwwform类不支持解析中文

于是转用httpwebrequest发送请求

目的:

客户端请求服务端拿中文正文

关键点:

1.httpwebrequest类的构造,参数设置,正文写入

2.GetResponse()接收响应正文,走streamreader解析,设置好encoding

详细代码:


    private IEnumerator WebRequestPost(string url, Dictionary<string, string> dictionary = null)
    {
        byte[] bytes = Encoding.GetEncoding("gb2312").GetBytes("test001=中文&test002=英文");
        HttpWebRequest request = new HttpWebRequest(new Uri(url));
        request.Method = "POST";
        request.ContentLength = bytes.Length;
        request.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
        using (Stream stream = request.GetRequestStream())
        {
            stream.Write(bytes, 0, bytes.Length);
        }
        yield return request.GetResponse();
        WebResponse res = request.GetResponse();
        Stream responstream = res.GetResponseStream();
        StreamReader reader2 = new StreamReader(responstream, Encoding.GetEncoding("gb2312"));
        string str = reader2.ReadToEnd();
        reader2.Close();
        responstream.Close();
        res.Close();
        Debug.Log(str);
    }

原创粉丝点击