The '', Hexadecimal value 0x1F, is not a v

2019-07-25 00:53发布

问题:

I am getting xml data through an httpwebrequest. The following code was working fine. But something changed and suddenly started to give me an exception on Read() method with error : The '', Hexadecimal value 0x1F, is not a valid character. Line 1, Item 1. In web browser sURL gives me a valid xml. I dont know what changed.

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sURL);
    req.Method = "GET";
    WebResponse response = req.GetResponse();
    StreamReader resStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
    XmlTextReader reader = new XmlTextReader(resStreamReader);
    while (reader.Read())
    {
       // some code
     }

回答1:

At This MSDN Topic Same problem discussed, and result is that,

Servers sometimes compress their responses to save on bandwidth, when this happens, you need to decompress the response before attempting to read it. Fortunately, the .NET framework can do this automatically, however, we have to turn the setting on.

And this stackoverflow topic solves the problem

0x1F XML Error Solution

You should check the response from your url about possible GZip compression usages.

Have a nice day.