[英]C#/Why does Get html returns random junk characters?
我有這個:鏈接
這段代碼:
const String nick = "Alex";
const String log = "http://demonscity.combats.com/zayavka.pl?logs=";
foreach (DateTime cd in dateRange)
{
string str = log + String.Format("{0:MM_dd_yy}", cd.Date) + "&filter=" + nick;
String htmlCode = wc.DownloadString(str);
}
返回一些東西...."‹\b\0\0\0\0\0\0я•XYsЫЦ~зЇёѕ™d)bг.тBҐ$ЪRЖ'<2УN&сh@р '„\f\0J–—_Фџђ§¤ нt¦г6ќѕУЄђ0'IQtТґcµо№X(jі-Щ/Ђі|g?`yҐ¶ц"
其他鏈接工作正常。 我認為問題出在代碼頁上,我該如何解決? 還是服務器問題?
問題是響應是 GZip 壓縮的(響應具有Content-Encoding: gzip
標頭)。 您需要先解壓縮它,然后才能閱讀它:
public class StackOverflow_6660689
{
public static void Test()
{
WebClient wc = new WebClient();
Encoding encoding = Encoding.GetEncoding("windows-1251");
byte[] data = wc.DownloadData("http://demonscity.combats.com/zayavka.pl?logs=08_07_11&filter=Alex");
GZipStream gzip = new GZipStream(new MemoryStream(data), CompressionMode.Decompress);
MemoryStream decompressed = new MemoryStream();
gzip.CopyTo(decompressed);
string str = encoding.GetString(decompressed.GetBuffer(), 0, (int)decompressed.Length);
Console.WriteLine(str);
}
}
我認為它以 gzip 格式返回結果,除非客戶端明確接受該格式,否則它不應該返回結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.