Click here to Skip to main content
15,886,016 members
Please Sign up or sign in to vote.
4.00/5 (1 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.

VB
' 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
    zipFileInfo.Directory.Create()
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)

Try
    ' 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.
        binaryWriter.Write(buffer)
        binWriter.Write(buffer)

        Console.WriteLine(buffer.Length)

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

    End While
Finally
    ' Clean up.
    binaryWriter.Close()
    binaryReader.Close()
    fileStream.Close()
    httpStream.Close()
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.

Thanks,
Carolyn
Posted
Comments
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

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.

Thanks,

Carolyn
 
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