- 接收端通过Request.InputStream读取流
public static string StreamRead()
{
byte[] byts = new byte[HttpContext.Current.Request.InputStream.Length];
HttpContext.Current.Request.InputStream.Read(byts, 0, byts.Length);
string req = System.Text.Encoding.UTF8.GetString(byts);
return HttpContext.Current.Server.UrlDecode(req);
}
/// <summary>
/// 获得请求报文转换成字符串
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static string getRequestBody(HttpRequestBase request)
{
string result = "";
using (Stream st = request.InputStream)
{
StreamReader sr = new StreamReader(st, Encoding.UTF8);
result = sr.ReadToEnd();
}
return result;
}
测试:
//Get;
// LaoBai/GetMsg
/// <summary>
/// 获取新订单信息
/// </summary>
/// <returns></returns>
public string GetMsg()
{
string json = "{\"code\":1,\"msg\":\"其他错误\"}";
string postString = string.Empty;
using (Stream stream = System.Web.HttpContext.Current.Request.InputStream)
{
Byte[] postBytes = new Byte[stream.Length];
stream.Read(postBytes, 0, (Int32)stream.Length);
postString = Encoding.UTF8.GetString(postBytes);
LogHelper.WriteBarcodesLog("消息内容:" + postString, "LaoBaiPush");
if (!string.IsNullOrEmpty(postString))
{
NewOrderNotification newOrderInfos = JsonConvert.DeserializeObject<NewOrderNotification>(postString);
if (newOrderInfos!=null)
{
json = "{\"code\":0,\"msg\":\"" + newOrderInfos + "\"}";
LogHelper.WriteBarcodesLog("接收成功,保存状态:" + postString, "youzanpush");
}
else
{
json = "{\"code\":1,\"msg\":\"" + postString + "\"}";
LogHelper.WriteBarcodesLog("测试消息,不处理。" + postString, "youzanpush");
}
}
else
{
json = "{\"code\":1,\"msg\":\"空数据\"}";
LogHelper.WriteBarcodesLog("无数据。" + postString, "youzanpush");
}
}
return json;
}