0

I'm trying to get the contents of a hosted .txt file on a website and compare it to a string or "key". Both strings read as the same however when compared against each other in an if statement to see if they match, they apparently do not.

For example, the contents of http://1.2.3.4/test.txt is 'hello'

using System;
using System.IO;
using System.Net;


namespace TestProgram
{
    public class Program
    {
        static void Main(string[] args)
        {
            string key = "hello";
            WebRequest request = WebRequest.Create("http://1.2.3.4/test.txt");
            WebResponse response = request.GetResponse();
            Stream data = response.GetResponseStream();
            string html = String.Empty;
            using (StreamReader sr = new StreamReader(data))
            {
                html = sr.ReadToEnd();
            }
            if (html == key)
            {
                Console.WriteLine("True");
                Console.ReadKey();
            }          
        }
    }  
}

I've tried html.Equals(key) and that doesn't work either. I've used WebClient as well and get the same result.

Ryvik
  • 363
  • 1
  • 3
  • 14

1 Answers1

1

It would be better if you bring your txt file as an example, I suppose there is some character escapes in the file, such as \n (new line) so you should trim, or replace this new line character from your response text first, before you check the equality:

Trim:

html.Trim()

Replace:

html.Replace(System.Environment.NewLine, string.Empty);

also, see this question

godot
  • 3,422
  • 6
  • 25
  • 42