我正在尝试检索网页的源代码,并且我正在使用 HTTPWebRequest 但仅返回以下内容:
<script type="text/javascript">
window.location="index.php";
</script>
我使用 fiddler 来比较 chrome 获取网页并将其与我的代码检索的内容进行比较。
左边是 Chrome,右边是 VS 代码
http://i.stack.imgur.com/Hgk9w.png
我注意到的是,内容长度没有 chrome 返回的那么大。我的代码内容长度通常在 70 位左右,而 chrome 通常会返回 87000 位。
我尝试过使用流和内存流。有人可以指出我正确的方向吗?
下面是我的功能:
public string GetAllCampaings()
{
string campaigns = null;
byte[] result;
byte[] buffer = new byte[4096];
HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create("http://magiclampmarketing.com/sms/manage_groups.php");
httpWebRequest2.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
httpWebRequest2.Method = "GET";
httpWebRequest2.CookieContainer = cookieContainer;
httpWebRequest2.KeepAlive = true;
httpWebRequest2.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11";
httpWebRequest2.Referer = "http://magiclampmarketing.com/sms/main.php";
httpWebRequest2.SendChunked = false;
WebHeaderCollection myWebHeaderCollection = httpWebRequest2.Headers;
myWebHeaderCollection.Add("Accept-Language", "en;q=0.8");
myWebHeaderCollection.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
myWebHeaderCollection.Add("Accept-Encoding", "gzip,deflate,sdch");
var sp = httpWebRequest2.ServicePoint;
var prop = sp.GetType().GetProperty("HttpBehaviour", BindingFlags.Instance | BindingFlags.NonPublic);
prop.SetValue(sp, (byte)0, null);
using (WebResponse response = httpWebRequest2.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (MemoryStream memoryStream = new MemoryStream())
{
int count = 0;
do
{
count = responseStream.Read(buffer, 0, count);
memoryStream.Write(buffer, 0, count);
} while (count != 0);
result = memoryStream.ToArray();
}
}
}
return campaigns;
}