Vb.net 从Web服务返回的字节数组中读取数据
我有一个web服务,它以字节数组返回数据。现在我想在我的控制台项目中读取该数据。如何做到这一点,我已经添加了所需的引用来访问该web服务。我正在使用vb.net VS2012。谢谢。我的web服务方法如下Vb.net 从Web服务返回的字节数组中读取数据,vb.net,Vb.net,我有一个web服务,它以字节数组返回数据。现在我想在我的控制台项目中读取该数据。如何做到这一点,我已经添加了所需的引用来访问该web服务。我正在使用vb.net VS2012。谢谢。我的web服务方法如下 Public Function GetFile() As Byte() Dim response As Byte() Dim filePath As String = "D:\file.txt" response = File.ReadAllBy
Public Function GetFile() As Byte()
Dim response As Byte()
Dim filePath As String = "D:\file.txt"
response = File.ReadAllBytes(filePath)
Return response
End Function
大概
Dim result As String
Using (Dim data As New MemoryStream(response))
Using (Dim reader As New StreamReader(data))
result = reader.ReadToEnd()
End Using
End Using
如果你知道编码,就说是UTF-8
Dim result = System.Text.UTF8Encoding.GetString(response)
根据你的评论,我认为你是在断言这一点
Dim response As Byte() 'Is the bytes of a Base64 encoded string.
因此,我们知道所有字节都是有效的ASCII(因为它的Base64),所以字符串编码是可交换的
Dim base64Encoded As String = System.Text.UTF8Encoding.GetString(response)
现在,base64Encoded
是一些二进制文件的字符串Base64表示形式
Dim decodedBinary As Byte() = Convert.FromBase64String(base64Encoded)
因此,我们将编码的base64更改为它所表示的二进制。现在,因为我可以在您的示例中看到,您正在读取一个名为“D:/file.txt”
的文件,所以我将假设该文件的内容是一个字符编码的字符串,但我不知道该字符串的编码。StreamReader
类在构造函数中有一些逻辑,可以对字符编码进行有根据的猜测
Dim result As String
Using (Dim data As New MemoryStream(decodedBinary))
Using (Dim reader As New StreamReader(data))
result = reader.ReadToEnd()
End Using
End Using
希望现在
result
包含文本文件的上下文。您希望显示的外观如何?Web服务如何对数据进行编码?这个文件是文本文件吗?文件使用什么编码?我想在控制台中读取web服务返回的文件内容。编码为Base64Binary。因此,web服务返回一个Byte()
,其中包含Base64编码的String
字节。请帮帮我。我很紧张。在您的评论之后,我已经扩展了答案,你的问题到底是什么还不清楚。