00.Forums
Re: binary files from the web
Started by Panacea at 10-20-2006 8:53 AM. Topic has 1 replies.
  Page 1 of 1 (2 items)

Panacea
Panacea
Added: 8:53 AM on 10/20/2006
<p>could you please tell me how to download the contents of a binary file found on the web into a string in c#. the code below works with text and html files but it does not properly download binary files</p>
<code>
***********************
// library imports ommited <br>

System.Net.HttpWebRequest webRequest = <br>
(System.Net.HttpWebRequest) System.Net.WebRequest.Create(url);<br>

System.Net.HttpWebResponse webResponse = <br>
(System.Net.HttpWebResponse) webRequest.GetResponse( );<br>
// get the streamReader from the response<br>

System.IO.BinaryReader streamReader = new System.IO.BinaryReader(<br>
webResponse.GetResponseStream( ));
string outputString="";<br>
int BUFFER_SIZE = 4096;<br>
Char[] buf = new Char[BUFFER_SIZE];<br>
int n = streamReader.Read ( buf, 0, BUFFER_SIZE );<br>
while ( n > 0 )<br>
{<br>
outputString += new String ( buf, 0, n );<br>
n = streamReader.Read ( buf, 0, BUFFER_SIZE );<br>
}<br>
**************************
</code>

shiono
shiono
Added: 9:08 PM on 10/20/2006

The code below download the files on the web.

/******************** begin of the code ********************/
            byte[] buf = new byte[BUFFER_SIZE];
            System.IO.FileStream stream = new System.IO.FileStream(filename, System.IO.FileMode.Create);
            int n = streamReader.Read(buf, 0, BUFFER_SIZE);
            while (n > 0) {
                stream.Write(buf, 0, n);
                n = streamReader.Read(buf, 0, BUFFER_SIZE);
            }
            stream.Close();
--- snip ---
/******************** end of the code ********************/

However, I do not know what you want to do. Char in C# represents the Unicode character and it is different from the char in C/C++, which can be used for representing 1 byte data.  Do you want to generate HEX dump of binary data?


theSpoke.net » English Topics » Coding » Re: binary files from the web
© Copyright 2005 Microsoft Corporation. All Rights Reserved.
Terms of Use | Privacy Statement | Code of Conduct | Hosted by MaximumASP for Microsoft
WHO-BAR