[英]How to get HTML data from a webpage using C# when page requires log in
因此,基本上,我希望從網頁上獲取HTML數據-問題是訪問該頁面需要登錄。我已經登錄了瀏覽器(IE),但我認為我的代碼未引用同一瀏覽器這就是為什么它需要登錄。
這是我到目前為止所做的:
public void HTMLImport(){
string urlAddress = "https://randomWebsite.com/reports/show_report.aspx";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if(response.StatusCode == HttpStatusCode.OK){
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
if(response.CharacterSet == null){
readStream = new StreamReader(receiveStream);
}
else{
readStream = new StreamReader(receiveStream,Encoding.GetEncoding(response.CharacterSet));
}
string data = readStream.ReadToEnd();
response.Close();
readStream.Close();
// This is showing the HTML data for when person is not logged in -
Console.WriteLine(data);
}
您將需要使用C#代碼執行登錄,也許是通過使用正確的憑據將登錄表單重新發布回服務器(太長時間才能在此處編寫代碼),然后再讀回響應頁面以獲取會話Cookie(大多數登錄功能將回復您必須包含在其他請求中的身份驗證Cookie)。
沒有更多的細節,很遺憾,我無法提供更多幫助。
為此,首先,您應該知道網站通常使用cookie來保存會話。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.