本文介绍了输入流在mvc 4 asp.net中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 对于MVC 4中的Web API我试图在控制器中获取http resquest post的输入流我总是得到字节数组的长度= 53但是字节数组是空的所以当我转换时它变成了一个字符串我继续得到一个空字符串 这就是我正在使用的: Dim InStream As Stream Dim Len 作为 整数 InStream = HttpContext.Current.Request .InputStream InStream = CType (InStream,Stream) Len = CInt ( InStream.Length) Dim ByteArray(Len) As 字节 InStream.Read(ByteArray, 0 ,Len) Dim js onParam As String jsonParam = System.Text.Encoding.UTF8.GetString(ByteArray) i认为它来自这一行: InStream = HttpContext.Current.Request.InputStream 但我没有找到如何更换它 请注意客户端发送的字符串是一个JSON字符串 任何帮助?解决方案 I发现流是在最后,我将必须将其设置为开头 Dim InStream As Stream Dim Len As Integer InStream = HttpContext.Current.Request.InputStream InStream = CType (InStream,Stream) Len = CInt (InStream.Length) Dim ByteArray(Len)作为 字节 InStream.Seek( 0 ,SeekOrigin。开始); InStream.Read(ByteArray, 0 ,Len) Dim jsonParam As String jsonParam = System.Text.Encoding.UTF8.GetString(ByteArray) for a web API in MVC 4 i'm trying to get the input stream of a http resquest post in the controller i'm always getting the length of the byte array = 53 but the byte array is empty so when i convert it to string i keep getting an empty stringthis is what i'm using:Dim InStream As StreamDim Len As IntegerInStream = HttpContext.Current.Request.InputStreamInStream = CType(InStream, Stream)Len = CInt(InStream.Length)Dim ByteArray(Len) As ByteInStream.Read(ByteArray, 0, Len)Dim jsonParam As StringjsonParam = System.Text.Encoding.UTF8.GetString(ByteArray)i think it's from this line :InStream = HttpContext.Current.Request.InputStreambut i did not find how to replace itnote that the string the client is sending is a JSON stringany help? 解决方案 I found that the stream is at the end, i will have to set it to the beginningDim InStream As StreamDim Len As IntegerInStream = HttpContext.Current.Request.InputStreamInStream = CType(InStream, Stream)Len = CInt(InStream.Length)Dim ByteArray(Len) As ByteInStream.Seek(0, SeekOrigin.Begin);InStream.Read(ByteArray, 0, Len)Dim jsonParam As StringjsonParam = System.Text.Encoding.UTF8.GetString(ByteArray) 这篇关于输入流在mvc 4 asp.net中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-16 06:33