LINGc_123 2016-07-28 02:01 采纳率: 0%
浏览 1266

C#获取请求报文主体,如果有等号,汉字会出现乱码

用post给服务器发报文,在服务器端(用C#写的)获取请求报文的主体,如果主体中有“=”,那么往后的汉字都是乱码,如果没有“=”,获得的主体就是正确的。而且“=”前面有奇数个汉字,显示都正常,有偶数个汉字,“=”后的就显示不正常
static void Main(string[] args)
{
using (HttpListener listerner = new HttpListener())
{
listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
listerner.Prefixes.Add(@"http://192.168.1.155/");
listerner.Start();
Console.WriteLine("WebServer Start Successed.......");

while (true)
{
HttpListenerContext ctx = listerner.GetContext();
ctx.Response.StatusCode = 200;
string httptype = ctx.Request.HttpMethod;
Console.WriteLine("-------------------------------------------");
Console.WriteLine(httptype);
string name = ctx.Request.QueryString["name"];
System.IO.StreamReader myreader = new System.IO.StreamReader(ctx.Request.InputStream, System.Text.UTF8Encoding.UTF8);
string text1 = myreader.ReadToEnd();
if (name != null)
{
Console.WriteLine(name);

}
Console.WriteLine(text1);
using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream))
{ //响应

writer.Write("发送成功");
writer.Close();
ctx.Response.Close();
}

            }
        }
    }

图片说明

  • 写回答

1条回答 默认 最新

  • w362335821 2016-07-28 03:20
    关注

    请求方应该对发送的内容做URLEnCode,
    在服务瑞的代码Request 对应应该有一个设置编码的方法,
    在执行string name = ctx.Request.QueryString["name"]; 之前 设置Request编码为UTF-8即可

    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配