首页 新闻 会员 周边

中文字符串转UTF-8的问题

0
悬赏园豆:10 [已解决问题] 解决于 2018-05-21 10:52

求助一下大神,现有个需求是调用外部接口,其中post的参数是要utf-8编码格式,但是我在网上搜的很多例子基本都是这样,代码如下:

/// <summary>
        /// 将字符串转utf-8
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private string GetUTF8(string str)
        {
            UTF8Encoding utf8 = new UTF8Encoding();
            Byte[] encodedBytes = utf8.GetBytes(str);
            return utf8.GetString(encodedBytes);
        }

但是转换后结果什么都没有改变,对这方面不是很懂,求指点一二!!!

例如:我想将“我要转成utf-8”转换成这种码“&#x6211;&#x8981;&#x8F6C;&#x6210;utf-8”,但是代码不奏效

问题补充:

我把接口代码发出来,麻烦大家参谋参谋

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://xxx.xxxxx.xxx/api/wx-message/push-message");
            request.Method = "POST";
            request.ContentType = "application/json";
            request.ContentLength = PostData.Length;
            WebHeaderCollection headers = request.Headers;
            headers.Add("MytPPSId", "xxxx");
            headers.Add("MytPPSSecret", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
            StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII);
            writer.Write(PostData);
            writer.Flush();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string encoding = response.ContentEncoding;
            if (encoding == null || encoding.Length < 1)
            {
                encoding = "UTF-8"; //默认编码  
            }

            //发送成功后接收返回的XML信息
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
            string retString = reader.ReadToEnd();
            JObject json = (JObject)JsonConvert.DeserializeObject(retString);
            string errcode = json["userData"]["errcode"].ToString();
            string errmsg = json["userData"]["errmsg"].ToString();
            if (errmsg == "ok")
            {
                result = true;
            }
            return result;

就是这个data不知道要传什么样的格式

单纯的小白的主页 单纯的小白 | 初学一级 | 园豆:108
提问于:2018-05-18 17:17

StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII);
writer.Write(PostData);
writer.Flush();
修改为Encoding.UTF8.不过英文的UTF8和ASCII编码内容是一样的。

贪心狸猫 5年前
< >
分享
最佳答案
0

很明显概念没搞清楚。多半是需要GBK转UTF8,而你这里string str进来就是utf8的话当然 没任何改变。

收获园豆:2
花飘水流兮 | 专家六级 |园豆:13560 | 2018-05-18 17:27

请问应该如果处理?我如果直接参数用json传输,不做任何改变的话,微信那边接收到的消息中文全是???????,英文和数字能正常显示

单纯的小白 | 园豆:108 (初学一级) | 2018-05-18 17:36

@单纯的小白: 看对方的文档要求是否有编码要求。如果没有说明,那么你在输出的时候在http头说明编码方式。或者尝试输出gbk字符串,对方很可能就认识了。

&#x6211;&#x8981;&#x8F6C;&#x6210;utf-8”属于html范畴,编码:ISO_8859_1

花飘水流兮 | 园豆:13560 (专家六级) | 2018-05-18 18:07
其他回答(5)
0

 文档说明呢 不会是传16进制吧

收获园豆:2
凡人修仙迹 | 园豆:369 (菜鸟二级) | 2018-05-18 17:35

实在是没办法才来求助的,没有任何文档,只能自己摸,痛苦啊

支持(0) 反对(1) 单纯的小白 | 园豆:108 (初学一级) | 2018-05-18 17:36

@单纯的小白: 你先看你接收是什么格式的数据啊 再转换 

支持(0) 反对(0) 凡人修仙迹 | 园豆:369 (菜鸟二级) | 2018-05-18 17:38

@凡人修仙迹: 接口只有使用权,一个地址,无法调试接收

支持(0) 反对(0) 单纯的小白 | 园豆:108 (初学一级) | 2018-05-18 17:39

@单纯的小白: 不能调试 就先不管什么格式的啊   继续往下写

支持(0) 反对(0) 凡人修仙迹 | 园豆:369 (菜鸟二级) | 2018-05-18 17:43

@凡人修仙迹: 就剩这个问题了,搞得本周都交差不了,烦躁

支持(0) 反对(0) 单纯的小白 | 园豆:108 (初学一级) | 2018-05-18 17:46

@单纯的小白: 你同事啊问 

支持(0) 反对(0) 凡人修仙迹 | 园豆:369 (菜鸟二级) | 2018-05-18 17:56
0


你的 &#x6211;&#x8981;&#x8F6C;&#x6210;utf-8 这种应该是HtmlEncoder编码方式吧

收获园豆:2
让我发会呆 | 园豆:2929 (老鸟四级) | 2018-05-18 18:00

HtmlEncoder.encode("我要转成utf-8")
输出结果:&#25105;&#35201;&#36716;&#25104;utf-8

好像也不和你要求输出的一样撒?

支持(0) 反对(0) 让我发会呆 | 园豆:2929 (老鸟四级) | 2018-05-18 18:02
0

string str;//默认格式,比如unicode

UTF8Encoding utf8 = new UTF8Encoding();

Byte[] encodedBytes = utf8.GetBytes(str);//utf8格式数据

return utf8.GetString(encodedBytes);//默认格式,unicode

应该返回函数改为byte[]类型。

return utf8.GetBytes(str);

收获园豆:2
贪心狸猫 | 园豆:872 (小虾三级) | 2018-05-18 18:00
0

https://files.cnblogs.com/files/jellochen/NLib.rar

使用方法见单元测试。如果编译报错,估计是C#新语法问题,可改成老语法。

收获园豆:2
jello chen | 园豆:7336 (大侠五级) | 2018-05-19 21:31
0

问题解决了,输入用byte,感谢各位老铁,下面贴出代码

 1  public bool SendMessage(string PostData)
 2         {
 3             bool result = false;
 4             HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://**************");
 5             request.Method = "POST";
 6             request.ContentType = "application/json";
 7             byte[] postBytes = Encoding.UTF8.GetBytes(PostData);
 8             request.ContentLength = Encoding.UTF8.GetBytes(PostData).Length;
 9             WebHeaderCollection headers = request.Headers;
10             headers.Add("MytPPSId", "****");
11             headers.Add("MytPPSSecret", "****");
12 
13             using (Stream reqStream = request.GetRequestStream())
14             {
15                 reqStream.Write(postBytes, 0, postBytes.Length);
16             }
17             return result;
18         }
单纯的小白 | 园豆:108 (初学一级) | 2018-05-21 10:51
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册