Click here to Skip to main content
13,356,318 members (64,984 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Okay, I'm working with a Bing Ads API/Service and I'm trying to pull data from a report. I'm using HTTPS to retrieve the data as I have to send a username and password along with some parameters. If I save the response as a file, it converts the data correctly. What I'm trying to do is read the data into a memory stream and pull out the pieces I want from there and then save to our database.

When I try to read the binary data into a memory stream I get either an empty string or the following characters "PK . There's some other characters not pasting in one being a paragraph symbol. Sometimes I can get a whole screen of garbleygook characters that convert to the above characters when I try to copy paste them. I think I'm not converting the binary string correctly.

Below is the code that I'm using to retrieve the data and convert to a string at this point.

' Open a connection to the URL where the report is available.
Dim webRequest As HttpWebRequest = Net.WebRequest.Create _(statusResponse.ReportRequestStatus.ReportDownloadUrl)
Dim response = CType(webRequest.GetResponse, HttpWebResponse)
Dim httpStream = response.GetResponseStream

' Open the destination file.
Dim zipFileInfo = New FileInfo(zipFileName)

If Not zipFileInfo.Directory.Exists Then
End If

Dim fileStream = New FileStream(zipFileInfo.FullName, FileMode.Create)
Dim ms = New MemoryStream
Dim binaryWriter = New BinaryWriter(FileStream)
Dim binWriter = New BinaryWriter(ms)
Dim binaryReader = New BinaryReader(httpStream)

    ' Read the report, and then write it to the destination file.
    Dim bufferSize As Integer = 100000

    While True
        ' Read the report data.
        Dim buffer() = binaryReader.ReadBytes(bufferSize)

        ' Write the report data to the destination file.


        ' If the end of the report is reached, break out of the
        ' loop.
        If (buffer.Length <> bufferSize) Then
            Exit While
        End If

    End While
    ' Clean up.
End Try

Dim reader As New StreamReader(ms, System.Text.Encoding.UTF8)
Dim xmlString As String = reader.ReadToEnd()
Console.WriteLine(xmlString) 'just using this to see what is in the string

Can anyone give me some pointers on what isn't correct about this? I've tried goggling about memorystreams and haven't found anything that is making a difference.

Posted 4-Jan-13 7:53am
ChrisTopherus 4-Jan-13 13:15pm
i can only give you a hint. in wcf, you can declare a request- and a responseformat. maybe there is something like that for your snippet...

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Okay. Got this figured out. The http response was not in xml as I thought but was a zipped file. By using DotNetZip, I was able to read the http response into an memory stream and convert that to the xml document all while in memory.



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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.180111.1 | Last Updated 7 Jan 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100