Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

VB
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 :

VB
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?
Posted

1 solution

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

VB
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)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900