Uri.UnescapeDataString fails on different computer

2019-05-03 03:55发布

On my dev computer everything was working fine and dandy but when I tested the program on a different Windows7 computer I was getting a System.UriFormatException: Invalid URI: There is an invalid sequence in the string. On the following code: Uri.UnescapeDataString(section);

At first I thought the second computer was receiving different data from the dev pc, so I copied the html string that was failing to a file and reduced my code to this:

static void Err(string s){/*Picked up by external logging*/}

private static void GetValue()
{
    try
    {
        var html = File.ReadAllText("ld.txt");

        //Retrieve section we want
        var section = Regex.Match(
            html,
            "etc_etc(.*): ",
            RegexOptions.Singleline)
                                .Groups[1].ToString();

        Uri.UnescapeDataString(section);
    }
    catch (Exception ex)
    {
        Err(ex.ToString());
    }
}

Works fine on the dev pc, but the second computer receives the exception again. They're both loading the exact same html from the exact same ld.txt file and then doing the exact same thing with it.. and both PC's are x64 Win7. What gives?

1条回答
欢心
2楼-- · 2019-05-03 04:24

UnescapeDataString appears to have changed between CLR 4.0 and 4.5

I can reproduce this on two machines with different versions of .Net installed (even though compiled to target .Net 4.0).

Do you perhaps have VS 2012 on your dev machine (and therefore .Net 4.5) and just .Net 4 on the other computer?

This code throws an exception on .Net 4.0, passes on .Net 4.5

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("CLR version: " + Environment.Version);
        Console.WriteLine(Uri.UnescapeDataString("%"));
    }
}
查看更多
登录 后发表回答