且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

输入流在mvc 4 asp.net中

更新时间:2023-02-16 10:50:22

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 string

this is what i'm using:

Dim InStream As Stream
Dim Len As Integer
InStream = HttpContext.Current.Request.InputStream
InStream = CType(InStream, Stream)
Len = CInt(InStream.Length)
Dim ByteArray(Len) As Byte
InStream.Read(ByteArray, 0, Len)
Dim jsonParam As String
jsonParam = System.Text.Encoding.UTF8.GetString(ByteArray)


i think it's from this line :

InStream = HttpContext.Current.Request.InputStream


but i did not find how to replace it

note that the string the client is sending is a JSON string

any help?

I found that the stream is at the end, i will have to set it to the beginning

Dim InStream As Stream
Dim Len As Integer
InStream = HttpContext.Current.Request.InputStream
InStream = CType(InStream, Stream)
Len = CInt(InStream.Length)
Dim ByteArray(Len) As Byte
InStream.Seek(0, SeekOrigin.Begin);
InStream.Read(ByteArray, 0, Len)
Dim jsonParam As String
jsonParam = System.Text.Encoding.UTF8.GetString(ByteArray)