开发者

Using WebClient.DownloadString() return odd characters sometimes [duplicate]

开发者 https://www.devze.com 2023-04-08 20:22 出处:网络
This question already has answers here: 开发者_如何学编程Closed 11 years ago. Possible Duplicate:
This question already has answers here: 开发者_如何学编程 Closed 11 years ago.

Possible Duplicate:

Characters in string changed after downloading HTML from the internet

To reproduce my issue have a look a the following code snippet:

const string url = "http://serienjunkies.org/serie/glee/";
var webclient = new WebClient();
var source = webclient.DownloadString(url);
Console.WriteLine(source.Substring(0, 200);

Sometimes (with absolutely the same code) I get the correct html source and othertimes just an unreadable string with odd characters. If you get the one - wait some seconds up to one minute - then you will get the other.

Expected result:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
...

The other result (copy, paste from cmd):

▼      ♥????????y]2?? em§?.f♦∟3Hf??J?XRM*???%??9 Gf¶##R1p?-??;?♥???♀???U?M?I?s &?#9???⌂?????fg????o????????????;}-????v⌂???^go????????zu???????tD?u^M?h:?
...

Thank you.

Kind regards, Danny


Basically the server is delivering content in a compressed (GZip) format.

The answer here Characters in string changed after downloading HTML from the internet will give you a replacement downloader that will handle compressed and uncompressed files.

0

精彩评论

暂无评论...
验证码 换一张
取 消