c# mvc 模式下接受 客户端 发过来的 body 值
跟手机客户端对接接口的时候 body 里面json数据 我在c#后台总是接收不到,同事提供了一个办法真是管用
//直接读取字节流 然自己拼接
public string ReadStream()
{
try
{
Stream reqStream = HttpContext.Current.Request.InputStream;
reqStream.Seek(0, SeekOrigin.Begin);
byte[] buffer = new byte[(int)reqStream.Length];
reqStream.Read(buffer, 0, (int)reqStream.Length);
string json = "";
foreach (char a in buffer)
{
json = json + a.ToString();
}
return json;
}
catch
{
}
return null;
}
//然后再用键值对的方式 吧需要的 参数读出来
public Dictionary<string, object> readLoginPara()
{
try
{
string json = ReadStream();
Dictionary<string, object> user = new Dictionary<string, object>();
user.Add("Tel", "");
user.Add("PassWord", "");
if (json != null)
{
JavaScriptSerializer js = new JavaScriptSerializer();
var jarr = js.Deserialize<Dictionary<string, object>>(json);
if (jarr != null)
{
foreach (var j in jarr)
{
if (j.Key == "Tel")
{ user["Tel"] = j.Value.ToString(); }
else if (j.Key == "PassWord")
{
user["PassWord"] = j.Value.ToString();
}
}
}
}
return user;
}
catch
{ }
return null;
}