2011-09-17 38 views
1

網頁抓取與https://livingsocial.comasp.net網頁抓取與https://livingsocial.com

目前,我用下面的代碼工作。

此代碼會將用戶authencation要求,但網頁不驗證用戶。

 string appURL = "https://livingsocial.com/deals/external_people/login"; 

     string strPostData = String.Format("login={0}&password={1}","[email protected]", "03006403836"); 

     // Setup the http request. 
     HttpWebRequest wrWebRequest = WebRequest.Create(appURL) as 
     HttpWebRequest; 
     //old code 
     wrWebRequest.ContentType = "application/x-www-form-urlencoded"; 
     wrWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)"; 
     wrWebRequest.Method = "POST"; 
     wrWebRequest.ContentLength = strPostData.Length; 
     wrWebRequest.ContentType = "application/x-www-form-urlencoded"; 
     CookieContainer cookieContainer = new CookieContainer(); 
     wrWebRequest.CookieContainer = cookieContainer; 

     // Post to the login form. 
     StreamWriter swRequestWriter = new 
     StreamWriter(wrWebRequest.GetRequestStream()); 
     swRequestWriter.Write(strPostData); 
     swRequestWriter.Close(); 

     // Get the response. 
     HttpWebResponse hwrWebResponse = (HttpWebResponse)wrWebRequest.GetResponse(); 

     // Read the response 
     StreamReader srResponseReader = new 
     StreamReader(hwrWebResponse.GetResponseStream()); 
     string strResponseData = srResponseReader.ReadToEnd(); 

回答

1

cookie容器必須與下一個webrequest一起使用。只有這樣,你會得到被記錄在的效果。

+0

我如何使用cookie容器的下一個請求告訴我與編碼。 – Wasif

1

您已經通過爲容器創建一個單獨的變量做最困難的部分。

CookieContainer cookieContainer = new CookieContainer(); 
wrWebRequest.CookieContainer = cookieContainer; 

就像Hasan表示您只需要不斷傳遞它與每個請求。

var newRequest = (HttpWebRequest)HttpWebRequest.Create("http://www.mysite.com/"); 
newRequest.CookieContainer = cookieContainer; 

var yetAnotherRequest = (HttpWebRequest)HttpWebRequest.Create("http://www.anothersite.com/"); 
yetAnotherRequest.CookieContainer = cookieContainer; 

這將在每個後續請求中保留cookie以及更可能是您的會話。

+0

我得到餅乾並保存。 (c)hwrWebResponse.Cookies中的Cookie c) { myContainer.Add(c); } 並用於這樣的新請求,但沒有響應那裏。 HttpWebRequest wrWebRequest1 = WebRequest.Create(appURL)as HttpWebRequest; wrWebRequest1.ContentType =「application/x-www-form-urlencoded」; 「wiwebRequest1.UserAgent =」Mozilla/4.0(compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)「; wrWebRequest1.CookieContainer = myContainer; – Wasif

+0

@Wasif,不需要遍歷cookie並將它們添加到您的容器中。如果你在兩次電話之間檢查你的容器,你會注意到所有的cookies都在那裏。您只需繼續將容器與每個請求一起傳遞。 – Josh