HTTPS Web响应未正确转换为xml [英] HTTPS Web Response not converting to xml correctly

查看:63
本文介绍了HTTPS Web响应未正确转换为xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在使用Bing Ads API / Service,我正在尝试从报告中提取数据。我正在使用HTTPS来检索数据,因为我必须发送用户名和密码以及一些参数。如果我将响应保存为文件,它会正确转换数据。我要做的是将数据读入内存流并从那里取出我想要的部分,然后保存到我们的数据库中。



当我尝试将二进制数据读入内存流时,我得到一个空字符串或以下字符PK ???。还有其他一些不会粘贴在一个字符符号中的字符。有时我可以获得整个屏幕的garbleygook字符,当我尝试复制粘贴它们时转换为上述字符。我想我不是正确地转换二进制字符串。



下面是我用来检索数据并在此时转换为字符串的代码。



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







谁能给我一些关于这个不正确的指示?我已经尝试过关注内存流并且找不到任何有用的东西。



谢谢,

Carolyn




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

推荐答案

好的。得到了这个想法。我认为http响应不是在xml中,而是一个压缩文件。通过使用DotNetZip,我能够将http响应读入内存流并将其转换为内存中的xml文档。



谢谢,



Carolyn
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


这篇关于HTTPS Web响应未正确转换为xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆