2010-04-15 60 views
1

我下載了一個網站的源代碼,但我下載了它,我看到它識別我的程序作爲一個客人,我在谷歌搜索,並找出我可以發送一個cookie時,我「問」的源代碼。 就是說我所能夠做到的,它仍然不認我爲註冊用戶:如何以使用c#的註冊用戶身份獲取網站源代碼?

CookieContainer cj = new CookieContainer(); 
string all = ""; 
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url); 
req.CookieContainer = cj; 
HttpWebResponse res = (HttpWebResponse)req.GetResponse(); 
CookieCollection cs=cj.GetCookies(req.RequestUri); 
CookieContainer cc = new CookieContainer(); 
cc.Add(cs); 
req.CookieContainer = cc; 
StreamReader read = new StreamReader(res.GetResponseStream()); 
all = read.ReadToEnd(); 
read.Close();    
return all; 

這裏有什麼問題? tyvm用於幫助:)

+0

有人知道嗎? – nir143 2010-04-15 20:57:51

回答

0

如果站點使用HTTP身份驗證,則可以嘗試以下方法:

req.PreAuthenticate=true; 
req.Credentials = new NetworkCredential("username", "password"); 

如果有一個自定義登錄表單,而不是,您將需要提交的數據和瀏覽器一樣。有一個在這裏使用WebRequest類將數據發送一些信息:

http://msdn.microsoft.com/en-us/library/debx8sh9.aspx

你可能會想改變該位:

string postData = "This is a test that posts this string to a Web server."; 

爲了這樣的事情:

string postData = "username=MyUsername&password=MyPassword"; 

將「用戶名」和「密碼」替換爲登錄表單上實際字段的名稱。

相關問題