asp.net

通过Request.InputStream读取流

2020-08-27

以下有两种方式可以获取响应的数据流


1. 接收端通过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);

}


2.

/// <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;

}