我有这个例子:链接
这个代码: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я•xyЫЦ~зЇёѕ™d) bг。тbҐЪR美元Ж’& lt; 2УN&сh@р’" f 0 j - _Фџђ§¤н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格式返回结果,除非客户端明确接受该格式。