源代码包含无效字符

时间:2013-12-18 22:11:57

标签: c# httpwebrequest invalid-characters

下面是一些假设只返回页面源代码的代码。然而,这个页面的源代码不断回来的意思是webdings和无效字符(数百个这些 )。我尝试了各种标题描述,但没有一个修复了问题。 “

string url2 = "http://mcassessor.maricopa.gov/?s=176-09-419"
HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(url2);

request2.CookieContainer = cookieJar;
request2.Method = "GET";
request2.Accept = "text/html, application/xhtml+xml, */*";
request2.Headers.Add("Accept-Language: en-US,en;q=0.5");
request2.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; MAM3; rv:11.0) like Gecko";
request2.Headers.Add("Accept-Encoding: gzip, deflate");
request2.Headers.Add("X-UA-Compatible: IE=edge,chrome=1");

using (HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse())
{
        string sourceCode2 = new StreamReader(request2.GetResponse().GetResponseStream()).ReadToEnd();
}

1 个答案:

答案 0 :(得分:4)

这是因为它回来了gzipped ..你告诉它在这里被压缩:

request2.Headers.Add("Accept-Encoding: gzip, deflate");

您可以删除它..或者,告诉请求解压缩它:

request2.AutomaticDecompression = DecompressionMethods.GZip;