2010-03-19 101 views
1

我想用下面的代碼驗證谷歌,但谷歌送我回到登錄頁面再次。谷歌認證的困難

//STEP# 1 
       string loginURL = "https://www.google.com/accounts/ServiceLoginBox?service=analytics&nui=1&hl=en-US&continue=https%3A%2F%2Fwww.google.com%2Fanalytics%2Fsettings%2F%3Fet%3Dreset%26hl%3Den%26et%3Dreset%26hl%3Den-US"; 
       request = (HttpWebRequest)WebRequest.Create(loginURL); 
       request.CookieContainer = cookieJar; 
       request.Method = "GET";    
       request.KeepAlive = true; 
       request.UserAgent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/2008111217 Fedora/3.0.4-1.fc10 Firefox/3.0.4"; 


       HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 


       foreach (Cookie cook in response.Cookies) 
       { 
         cookieJar.Add(cook); 
       } 


       using (StreamReader sr = new StreamReader(response.GetResponseStream())) 
       { 
        serverResponse = sr.ReadToEnd();      
        sr.Close(); 
       }   

       galx = ExtractValue(serverResponse,"GALX","name=\"GALX\" value=\""); 

       Console.WriteLine(galx); 


       //Request# 2 

       string uriWithData = "https://www.google.com/accounts/ServiceLoginBoxAuth"; 
       request = (HttpWebRequest)WebRequest.Create(uriWithData); 
       request.KeepAlive = true; 
       request.UserAgent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/2008111217 Fedora/3.0.4-1.fc10 Firefox/3.0.4"; 
       request.Method = "POST"; 
       request.CookieContainer = cookieJar; 
       string param = string.Format("Email={0}&Passwd={1}&continue={2}&service=analytics&nui=1&dsh=8209101995200094904&GALX={3}&hl=en-US&PersistentCookie=yes","**my email address**",p,"",galx); 
       byte[] postArr = StrToByteArray(param); 
       request.ContentType = @"application/x-www-form-urlencoded"; 
       request.ContentLength = param.Length; 

       Stream reqStream = request.GetRequestStream(); 
       reqStream.Write(postArr,0,postArr.Length); 
       reqStream.Close(); 

       response = (HttpWebResponse)request.GetResponse(); 


       foreach (Cookie cook in response.Cookies) 
       { 
         cookieJar.Add(cook); 
       } 


       using (StreamReader sr = new StreamReader(response.GetResponseStream())) 
       { 
        serverResponse = sr.ReadToEnd(); 

        Console.WriteLine(serverResponse); 
        // Close and clean up the StreamReader  
        sr.Close(); 
       }   
+0

你得到的錯誤是什麼?在女巫點不工作? – Aristos 2010-03-19 12:39:39

+0

你會得到什麼樣的異常/消息?什麼不起作用? – Younes 2010-03-19 12:40:12

+0

@ Younes,@ Aristos它讓我回到登錄頁面。 – developer 2010-03-19 12:44:24

回答

0

我不知道我很確定沒有多少人想要篩選那麼多的代碼。

我一眼就可以看到可能導致問題的一件事就是您玩的曲奇太多。

創建一個CookieContainer,並將它傳遞給每個請求。

無需「轉移」或「重新創建」容器。

+0

我只有一個cookiecontainer對象,我發送每個請求 – developer 2010-03-19 12:43:35

+0

@Sanders cookieJar對象範圍是全局的 – developer 2010-03-19 12:45:36

+0

@ user283405 - 無論您看到「cookieJar.Add(cook)」就是我所說的。不僅是不必要的,它可能是問題。 – 2010-03-19 12:50:09