使用 c# 到 webapi2 从 POST 数据中忽略“+”字符

'+' character ignored from POST data using c# to webapi2

我有一个客户端连接到 Asp.net Webapi2,使用身份和 OAuth2 进行身份验证。
在身份验证过程中,只要密码字段包含“+”character.The 服务器就忽略此字符!!!(以及下面测试中提到的大多数其他符号字符)

string data = "grant_type=password&username=" + username + "&password=" + password;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data);
data.PostToUrl();//This Is just pseudoCode

在服务器调试中:
发送数据:密码=test+1
收到的数据:密码=测试1
测试 2
已发送数据:“+_)(&^%$#@!~”
接收到的数据:" _)(
"

谢谢。

这是什么问题?对于 HTTP URL,+ 等同于 space。其实%20也可以用

在查询中发送数据时总是使用UrlEncode;如

var q = string.Format("grant_type=password&username={0}&password={1}", 
    HttpUtility.UrlEncode(username),
    HttpUtility.UrlEncode(password));

HttpServerUtility.UrlEncode

这将有助于解决特殊字符的问题,例如 + anad #

要使用它,您需要添加对 System.Web 的引用(Project Explorer > References > Add reference > System.Web)

完成后,您可以使用它来编码您想要的任何项目