0

I'm interested in pulling a file from online a .txt file.

The txt file stores:

filename md5 hash filename md5 hash

I am interested in getting the data from online then comparing the data to local files.

        byte[] buffer = new byte[512];
        WebRequest test = WebRequest.Create("http://www.domain.com/file.txt");
        Stream something = test.GetRequestStream();
        something.Read(buffer,0,20);

I don't quite understand streams and how to go about reading just one line from the file. I do not want to download the file first then retrieve the data. I'm interested in just pulling it from online. How different are "streams" vs normal IO, with StreamWriter and StreamReader?

EDIT--

       WebRequest myWebRequest = WebRequest.Create("http://www.domain.com/file.txt");

       WebResponse myReponse = myWebRequest.GetResponse();

       Stream recStream = myReponse.GetResponseStream();

       StreamReader reader = new StreamReader(recStream);

       txt_status.Text = reader.ReadLine();

3 Answers 3

2

GetRequestStream provides a stream for writing to. If you want the returned data to walk through make use of GetResponseStream

...

Stream ReceiveStream = myWebResponse.GetResponseStream();

    Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

        // Pipe the stream to a higher level stream reader with the required encoding format. 
     StreamReader readStream = new StreamReader( ReceiveStream, encode );
     Console.WriteLine("\nResponse stream received");
     Char[] read = new Char[256];

        // Read 256 charcters at a time.    
     int count = readStream.Read( read, 0, 256 );
        Console.WriteLine("HTML...\r\n");

    while (count > 0) 
    {
            // Dump the 256 characters on a string and display the string onto the console.
        String str = new String(read, 0, count);
        Console.Write(str);
        count = readStream.Read(read, 0, 256);
    }

...
Sign up to request clarification or add additional context in comments.

1 Comment

RoR needs line-by-line reading - ReadLine of reader would work better for that case.
2

If you're reading text, try using a TextReader

WebRequest test = WebRequest.Create("http://www.domain.com/file.txt");
Stream something = test.GetRequestStream();
TextReader reader = (TextReader)new StreamReader(something);
string textfile = reader.ReadToEnd();

1 Comment

+1 Ajma: no reason to cast StreamReader to TextReader as it is already TextReader. RoR: use ReadLine instead of ReadToEnd.
0

All a stream is, is a sequence of bytes. MemoryStreams, FileStream, etc. all inherit from System.IO.Stream

If you are simply attempting to compare the MD5 has against a local file, you could do something such as the following (not tested):

// Download File
WebClient wc = new WebClient();
byte[] bytes = wc.DownloadData();

MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] hash = md5.ComputeHash(bytes);

StringBuilder onlineFile = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
   onlineFile.Append(hash[i].ToString("X2"));
}

// Load Local File
FileStream fs = new FileStream(@"c:\yourfile.txt",FileMode.Open);
byte[] fileBytes = new byte[fs.Length];
fs.Read(fileBytes, 0, fileBytes.Length);

byte[] hash = md5.ComputeHash(fileBytes);

StringBuilder localFile = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
   onlineFile.Append(hash[i].ToString("X2"));
}


if(localFile.ToString() == onlineFile.ToString())
{
   // Match
}

1 Comment

StreamReaders, StreamWriters do not inherit from Stream.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.