我試圖獲取源代碼的網頁,我使用HttpWebRequest的,但僅此返回:我如何獲得HTTP web請求返回完整的源
<script type="text/javascript">
window.location="index.php";
</script>
我用小提琴手得到一個比較獲取網頁的鉻,並將其與我的代碼檢索的內容進行比較。
Chrome是在左邊,VS代碼右側
http://i.stack.imgur.com/Hgk9w.png
我已經注意到的是,內容長度是沒有地方大到什麼鍍鉻回來。我的代碼內容長度通常在70次左右,而鉻通常會返回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;
}
所有正在返回的JavaScript正在將瀏覽器立即重定向到index.php。所以你正確地閱讀了頁面的內容,這不僅僅是你期望的內容。 – Bobson
無論如何,即使JavaScript正在運行,我是否仍然可以獲取頁面內容?或httpwebrequests不處理JavaScript的?有沒有辦法讓我獲得頁面的源代碼?如果我打開chrome/IE/Firefox進入頁面,登錄並查看頁面源代碼? –
嘗試在瀏覽器中關閉javascript,然後訪問該頁面並查看源代碼。看看你給了什麼。但是,是的,HttpWebRequest根本不處理JavaScript。 – Bobson