locked
Loading GZIP InputStream Into XML Doc RRS feed

  • Question

  • User-186058747 posted

    I have a page that accepts XML, and I've always loaded it for parsing by simply doing: 

    Dim xDoc As XmlDocument = New XmlDocument()
    xDoc.Load(Request.InputStream)

    However the input stream is coming over as a string of digits, like so:

    60631201091083211810111411510511111061344....(many, many more)

    At first I thought it was base64, but that isn't the case.  How do I decode this?

    Monday, August 31, 2015 6:19 PM

All replies

  • User-37275327 posted

    Hope you can adapt the below code for  your requirement, code below turns an object into xml document and put it on memory stream.

    on the click event

      Dim currentStream As New MemoryStream()
      Dim streamWriter As New StreamWriter(currentStream)
      streamWriter.Write(CreateXMLDoc(reportDetail))
    
      streamWriter.Flush()
      currentStream.Position = 0
    

    Now the "currentStream" contains xml document content as string, Here "reportDetail" is the object which needed to converted to xml. alter this function as to your requirement.

        Public Shared Function CreateXMLDoc(ByVal value As Object) As String
            Dim rv As String = ""
            Dim xml = New XmlSerializer(value.[GetType]())
            Using memoryStream As MemoryStream = New MemoryStream()
    
                Using xmlWriter As XmlTextWriter = New XmlTextWriter(memoryStream, Encoding.UTF8)
                    xmlWriter.Formatting = Formatting.Indented
                    xml.Serialize(xmlWriter, value)
                    rv = Encoding.UTF8.GetString(memoryStream.ToArray())
                End Using
    
            End Using
            Return rv
        End Function

    you can use the currentStream variable to manipulate the xml,  like sending an email with xml attachment.

    Monday, August 31, 2015 11:08 PM
  • User-219423983 posted

    Hi kevorkian,

    You could first save the “Request.InputStream” to check whether the content is right.  Besides, you could try to use “Request.Files[0].InputStream” to get the HttpPostedFile. If the issue still exists, you could provide the whole code about how to save the xml files to Steam.

    Dim stream As New StreamReader(Request.Files(0).InputStream)
    Dim x As String = stream.ReadToEnd()
    Dim xml As String = HttpUtility.UrlDecode(x)
    Dim document = New XmlDocument()
    document.LoadXml(xml) 
    

    https://msdn.microsoft.com/en-us/library/system.web.httppostedfile.inputstream(v=vs.110).aspx

    I hope it’s useful to you.

    Best Regard,

    Weibo Zhang

    Tuesday, September 1, 2015 3:29 AM
  • User-186058747 posted

    Here is the post header:

    Header Info: Cache-Control=no-cache

    Connection=close

    Content-Length=413

    Content-Type=application/xml

    Accept=text/xml, application/xml, text/html, text/plain, */* Accept-Encoding=gzip, deflate, * Accept-Language=en-us, en, * Authorization=Basic Og== Host=myhost.com

    User-Agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; Win 9x 4.90; Q312461)

    The only method I have found effective to obtain anything from the inputstream, is this:

                Str = Request.InputStream
                strLen = CInt(Str.Length)
                Dim strArr(strLen) As Byte
                strRead = Str.Read(strArr, 0, strLen)
    
                For counter = 0 To strLen - 1
                    strmContents = strmContents & strArr(counter).ToString()
                Next counter


    The contents of strmContents is:

    6063120109108321181011141151051111106134494648343210111099111100105110103613485847045563463621310601101011191141119710011595100101991051151051111106213106097112112108105999711610511111095105100625655495248604797112112108105999711610511111095105100621310609911211595971121121081059997116105111110951051006257575748564548485050495560479911211595971121121081059997116105111110951051006213106010010199105115105111110951009711610162504849534548574548506047100101991051151051111109510097116101621310601001019910511510511111095116105109101624956585253584956604710010199105115105111110951161051091016213106010010199105115105111110626860471001019910511510511111062131060115116971161171156251496047115116971161171156213106011211110595114101991011051181011006277604711211110595114101991011051181011006213106011211110595118101114105102105101100627360471121111059511810111410510210510110062131060106111989511610510910195118101114105102105101100627360471061119895116105109101951181011141051021051011006213106047110101119114111971001159510010199105115105111110621310

    Wednesday, September 2, 2015 10:04 PM
  • User-219423983 posted

    Hi kevorkian,

    The only method I have found effective to obtain anything from the inputstream, is this:

    Did the method I provide you above have some issue on your client? Could you show the result of the above method? If the method also has the same issue, could you show how you accept the XML? Besides, you could also take a look at the following similar thread which shows how to get the xml file string.

    http://stackoverflow.com/questions/10049003/retrieve-http-post-request-containing-xml

    I hope it’s useful to you.

    Best Regards,

    Weibo Zhang

    Sunday, September 13, 2015 7:35 AM
  • User-186058747 posted

    Ok, so still can't figure this out.  Here is the post header:

    Cache-Control=no-cache Connection=close Content-Length=413 Content-Type=application/xml Accept=text/xml, application/xml, text/html, text/plain, */* Accept-Encoding=gzip, deflate, * Accept-Language=en-us, en, * Authorization=Basic Og== Host=myhost.com User-Agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; Win 9x 4.90; Q312461) 

    Here is how I am attempting to decode the stream:

     ' Create a Stream object.
                Str = Request.InputStream
                ' Find number of bytes in stream.
                strLen = CInt(Str.Length)
                ' Create a byte array. 
                Dim strArr(strLen) As Byte
                ' Read stream into byte array.
                strRead = Str.Read(strArr, 0, strLen)
    
                ' Convert byte array to a text string. 
                For counter = 0 To strLen - 1
                    strmContents = strmContents & strArr(counter).ToString()
                Next counter
    
    
                Dim textResponse = Decompress(strArr)
    Private Shared Function Decompress(data As Byte()) As Byte()
            Using compressedStream = New MemoryStream(data)
                compressedStream.Seek(0, SeekOrigin.Begin)
                Using zipStream = New GZipStream(compressedStream, CompressionMode.Decompress)
                    Using resultStream = New MemoryStream()
                        zipStream.CopyTo(resultStream)
                        Return resultStream.ToArray()
                    End Using
                End Using
            End Using
        End Function

    That function results in the exception:

    The magic number in GZip header is not correct. Make sure you are passing in a GZip stream. 


    Wednesday, September 16, 2015 4:54 PM