Exe File encoding - http via vb.net
-
I'm in the process of building a HTML server using vb.net which dose the job quite well. Currently I'm trying to add a function for downloading files. The sending of plain text works without a glitch (see code 1). However sending files such as executables (*.exe) or Excel (*.xls) files dosen't work (see code 2). The file once downloaded simply wont run. Upon closer examination (by opening the downloaded file in Notepad) signs of incorrect encoding are typically seen. there are a lot of question mark characters and when compared 1 on 1 with the original file it is clear that the encoding is the problem. Simply said I have no idea on how to resolve this and internet research isn't proving to be a reliable bron of information. Since the problem is one of encoding what I have tried is to read the exe file through a file stream-read and convert its context (probably binary) to text and send that with result that there there is an inprovment but the problem still persists and ultimatlhy the file still dosen't work. How do I resolve this? CODE 1
With My404HtmlPage
.Append("HTTP/1.1 200 OK" & Environment.NewLine) .Append("Content-Type: text/plain & Environment.NewLine) .Append("Content-Disposition: attachment; filename=" & Chr(34) & "Download.exe" & Chr(34) & Environment.NewLine) .Append(Environment.NewLine) .Append("Hello World!") End With
CODE 2
With My404HtmlPage
.Append("HTTP/1.1 200 OK" & Environment.NewLine) .Append("Content-Type: application/octet-stream & Environment.NewLine) .Append("Content-Disposition: attachment; filename=" & Chr(34) & "Download.exe" & Chr(34) & Environment.NewLine) .Append(Environment.NewLine) Dim bytes = My.Computer.FileSystem.ReadAllBytes("C:\\Original.exe") Dim MyString As String = Encoding.UTF8.GetString(bytes) .Append("content-bytes:" & MyString)
End With
-
I'm in the process of building a HTML server using vb.net which dose the job quite well. Currently I'm trying to add a function for downloading files. The sending of plain text works without a glitch (see code 1). However sending files such as executables (*.exe) or Excel (*.xls) files dosen't work (see code 2). The file once downloaded simply wont run. Upon closer examination (by opening the downloaded file in Notepad) signs of incorrect encoding are typically seen. there are a lot of question mark characters and when compared 1 on 1 with the original file it is clear that the encoding is the problem. Simply said I have no idea on how to resolve this and internet research isn't proving to be a reliable bron of information. Since the problem is one of encoding what I have tried is to read the exe file through a file stream-read and convert its context (probably binary) to text and send that with result that there there is an inprovment but the problem still persists and ultimatlhy the file still dosen't work. How do I resolve this? CODE 1
With My404HtmlPage
.Append("HTTP/1.1 200 OK" & Environment.NewLine) .Append("Content-Type: text/plain & Environment.NewLine) .Append("Content-Disposition: attachment; filename=" & Chr(34) & "Download.exe" & Chr(34) & Environment.NewLine) .Append(Environment.NewLine) .Append("Hello World!") End With
CODE 2
With My404HtmlPage
.Append("HTTP/1.1 200 OK" & Environment.NewLine) .Append("Content-Type: application/octet-stream & Environment.NewLine) .Append("Content-Disposition: attachment; filename=" & Chr(34) & "Download.exe" & Chr(34) & Environment.NewLine) .Append(Environment.NewLine) Dim bytes = My.Computer.FileSystem.ReadAllBytes("C:\\Original.exe") Dim MyString As String = Encoding.UTF8.GetString(bytes) .Append("content-bytes:" & MyString)
End With
Bart van Tuijl wrote:
Dim MyString As String = Encoding.UTF8.GetString(bytes)
No, apply them bytes? That's what Dave says and he is right. Bytes are that. No encoding. Write them bytes directly.
Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
-
Bart van Tuijl wrote:
Dim MyString As String = Encoding.UTF8.GetString(bytes)
No, apply them bytes? That's what Dave says and he is right. Bytes are that. No encoding. Write them bytes directly.
Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
Okay I get how that should work. I'm not all to sure on how to achieve this and I've given it a shot but it dosn't work as expected all the downloaded file now says when opened in notepad is 77 Here's my attempt.
With My404HtmlPage .Append("HTTP/1.1 200 OK" & Environment.NewLine) .Append("Content-Type: application/octet-stream" & Environment.NewLine) .Append("Content-Disposition: attachment; filename=" & Chr(34) & "Bilandted.exe" & Chr(34) & Environment.NewLine) .Append(Environment.NewLine) Dim array() As Byte = File.ReadAllBytes("C:\\Test\\TestFile.exe") Using memory As MemoryStream = New MemoryStream(array) Using reader As BinaryReader = New BinaryReader(memory) .Append((reader.ReadByte())) End Using End Using
End With
Im thinking I need to do something with the byte length but have now landed outside of my field of knowledge?
-
Okay I get how that should work. I'm not all to sure on how to achieve this and I've given it a shot but it dosn't work as expected all the downloaded file now says when opened in notepad is 77 Here's my attempt.
With My404HtmlPage .Append("HTTP/1.1 200 OK" & Environment.NewLine) .Append("Content-Type: application/octet-stream" & Environment.NewLine) .Append("Content-Disposition: attachment; filename=" & Chr(34) & "Bilandted.exe" & Chr(34) & Environment.NewLine) .Append(Environment.NewLine) Dim array() As Byte = File.ReadAllBytes("C:\\Test\\TestFile.exe") Using memory As MemoryStream = New MemoryStream(array) Using reader As BinaryReader = New BinaryReader(memory) .Append((reader.ReadByte())) End Using End Using
End With
Im thinking I need to do something with the byte length but have now landed outside of my field of knowledge?
What type is your
My404HtmlPage
variable? Hopefully, it should support writing more than one byte at a time... If not, you'll need to loop over the bytes and copy them all to the output one by one. NB: There's no need for theMemoryStream
andBinaryReader
, since you already have an array of the bytes you need to send.Dim array() As Byte = File.ReadAllBytes("C:\Test\TestFile.exe")
For i As Integer = 0 To array.Length - 1
.Append(array(i))
Next
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
What type is your
My404HtmlPage
variable? Hopefully, it should support writing more than one byte at a time... If not, you'll need to loop over the bytes and copy them all to the output one by one. NB: There's no need for theMemoryStream
andBinaryReader
, since you already have an array of the bytes you need to send.Dim array() As Byte = File.ReadAllBytes("C:\Test\TestFile.exe")
For i As Integer = 0 To array.Length - 1
.Append(array(i))
Next
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
There is indeed a lot of data now in the downloaded file but its still all in numbers (It dosent look like binary too me but more like decimal without the hyphens) what am I missing here I'm assuming that the browser would know how to handle this data and reproduce a valid file from it (perhaps I'm wrong)? also to answer the type of variable for My404HtmlPage is
Dim My404HtmlPage As New System.Text.StringBuilder()
which then is passed on to a variable named MyHtmlServerResponse as follows
MyHtmlServerResponse = (My404HtmlPage.ToString)
MyHtmlServerResponse is a string variable as can be seen below
Dim MyHtmlServerResponse As String
EDIT: Just as extra Info the actual sending is done through TCP/ IP as follows
For Each c As TcpClient In MyClientList
If c Is MyClient Then Dim nns As NetworkStream = c.GetStream() nns.Write(Encoding.ASCII.GetBytes(MyHtmlServerResponse), 0, MyHtmlServerResponse.Length) MyClient.Dispose() End If
-
I'm in the process of building a HTML server using vb.net which dose the job quite well. Currently I'm trying to add a function for downloading files. The sending of plain text works without a glitch (see code 1). However sending files such as executables (*.exe) or Excel (*.xls) files dosen't work (see code 2). The file once downloaded simply wont run. Upon closer examination (by opening the downloaded file in Notepad) signs of incorrect encoding are typically seen. there are a lot of question mark characters and when compared 1 on 1 with the original file it is clear that the encoding is the problem. Simply said I have no idea on how to resolve this and internet research isn't proving to be a reliable bron of information. Since the problem is one of encoding what I have tried is to read the exe file through a file stream-read and convert its context (probably binary) to text and send that with result that there there is an inprovment but the problem still persists and ultimatlhy the file still dosen't work. How do I resolve this? CODE 1
With My404HtmlPage
.Append("HTTP/1.1 200 OK" & Environment.NewLine) .Append("Content-Type: text/plain & Environment.NewLine) .Append("Content-Disposition: attachment; filename=" & Chr(34) & "Download.exe" & Chr(34) & Environment.NewLine) .Append(Environment.NewLine) .Append("Hello World!") End With
CODE 2
With My404HtmlPage
.Append("HTTP/1.1 200 OK" & Environment.NewLine) .Append("Content-Type: application/octet-stream & Environment.NewLine) .Append("Content-Disposition: attachment; filename=" & Chr(34) & "Download.exe" & Chr(34) & Environment.NewLine) .Append(Environment.NewLine) Dim bytes = My.Computer.FileSystem.ReadAllBytes("C:\\Original.exe") Dim MyString As String = Encoding.UTF8.GetString(bytes) .Append("content-bytes:" & MyString)
End With
Bart van Tuijl wrote:
I'm in the process of building a HTML server using vb.ne
Reading through this and your other posts. You need to come to a full stop on your code and first learn how http and the browser works. And additionally what a 'binary' file is. ------------------------------------------ Binary file - if is an executable and you open it in notepad will always look like you found. Does not matter where it came from. Notepad takes data and then transforms it in to characters. That can only work if the data is in fact characters. A binary, especially an executable will never consist solely of characters. So of course notepad will never display it because it cannot. Notepad displays a '?' for data that is not actually characters. Some (most) executables will have data in them that you will be able to read. Doesn't mean the rest is wrong just that an executable has text in it. And all of that depends on what character set notepad expects to see (I suspect you might want to research that also.) You can experiment with this by using notepad to open various files on your computer. I suggest 1. Executable 2. Image file 3. Write your own program, put text in it (a string), compile it, then open that. Do NOT save anything in Notepad to those above files. Unless you want to destroy them (try it if you wish but only if you expect to lose it.) If you really want to modify a binary file manually then you need to find a 'hex editor'. If you want to do that then you will need to learn what 'hex' means. ------------------------------------------ A browser sends a request to a web server and the web server then responds to that request. You should learn about the HTTP protocol. Your browser will have a 'developer tool' which allows you to inspect both requests and responses. The HTTP protocol itself does not support file downloads in anyway. A specific request might result in some content which originates from files being downloaded. 1. A html response (from a request) might have tags in it which the browser which will then use to make another request to the web server, and the response body with have data which is an image. The request defines this not the HTTP protocol. Browsers used to support a 'ftp' protocol that allowed files to be downloaded. Rather than 'http' it started with 'ftp' However apparently newer versions (Chrome, Firefox) no longer support that at all.
-
There is indeed a lot of data now in the downloaded file but its still all in numbers (It dosent look like binary too me but more like decimal without the hyphens) what am I missing here I'm assuming that the browser would know how to handle this data and reproduce a valid file from it (perhaps I'm wrong)? also to answer the type of variable for My404HtmlPage is
Dim My404HtmlPage As New System.Text.StringBuilder()
which then is passed on to a variable named MyHtmlServerResponse as follows
MyHtmlServerResponse = (My404HtmlPage.ToString)
MyHtmlServerResponse is a string variable as can be seen below
Dim MyHtmlServerResponse As String
EDIT: Just as extra Info the actual sending is done through TCP/ IP as follows
For Each c As TcpClient In MyClientList
If c Is MyClient Then Dim nns As NetworkStream = c.GetStream() nns.Write(Encoding.ASCII.GetBytes(MyHtmlServerResponse), 0, MyHtmlServerResponse.Length) MyClient.Dispose() End If
It does not matter what the content of the file looks like. If you do not understand how to handle binary data without corrupting it then you will never pass go or collect your $200. So stop trying to convdert the content into printable/displayable strings, that just destroys it (as I said in my previous message). You must read and write the content as pure binary bytes, nothing else.
-
There is indeed a lot of data now in the downloaded file but its still all in numbers (It dosent look like binary too me but more like decimal without the hyphens) what am I missing here I'm assuming that the browser would know how to handle this data and reproduce a valid file from it (perhaps I'm wrong)? also to answer the type of variable for My404HtmlPage is
Dim My404HtmlPage As New System.Text.StringBuilder()
which then is passed on to a variable named MyHtmlServerResponse as follows
MyHtmlServerResponse = (My404HtmlPage.ToString)
MyHtmlServerResponse is a string variable as can be seen below
Dim MyHtmlServerResponse As String
EDIT: Just as extra Info the actual sending is done through TCP/ IP as follows
For Each c As TcpClient In MyClientList
If c Is MyClient Then Dim nns As NetworkStream = c.GetStream() nns.Write(Encoding.ASCII.GetBytes(MyHtmlServerResponse), 0, MyHtmlServerResponse.Length) MyClient.Dispose() End If
Bart van Tuijl wrote:
also to answer the type of variable for My404HtmlPage is Dim My404HtmlPage As New System.Text.StringBuilder()
Well, there's your problem. You cannot treat binary data as a string!
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Bart van Tuijl wrote:
also to answer the type of variable for My404HtmlPage is Dim My404HtmlPage As New System.Text.StringBuilder()
Well, there's your problem. You cannot treat binary data as a string!
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
This My404HtmlPage being a string I had suspected, though Im at a dilemma because my http header information is sent as string and my file as bytes yet they belong to eachother. Is there a way I can convert my HTTP header to bytes and add my file to it as bytes and then send it or will that not work? (Sorry guys Im just a hobbyist so my knowledge is limited)
-
This My404HtmlPage being a string I had suspected, though Im at a dilemma because my http header information is sent as string and my file as bytes yet they belong to eachother. Is there a way I can convert my HTTP header to bytes and add my file to it as bytes and then send it or will that not work? (Sorry guys Im just a hobbyist so my knowledge is limited)
Yes as I suggested in my other post. You need to learn what hexadecimal is ('hex'). You need to get a hexadecimal editor (it will be called a hex editor.) You need to compile a SMALL project and then look at the exe (which is a binary file) using the hex editor. Not notepad. You need to then look at an image using a hex editor. You need to then find the specification ('spec') for that type of image and use the hex editor (not notepad) to figure out the parts of the image. I would also suggest that you write a program parse an image file. NOT display it. Just read it and print out the parts. 1. Read the file with a stream 2. Use the spec to figure out how to read the file. 3. Print out the various pieces. For example there is likely a size (x and y) so print that out as integers.