2010-05-20 17 views
5

मैं अपने .NET एप्लिकेशन से एक वेब ऐप में लॉग इन करने का प्रयास कर रहा हूं, लेकिन किसी कारण से यह काम नहीं करता है। यहाँ लॉगिन कोड है:HttpWebResponse लॉगिन

<form action="./process-login.php" method="post"> 
     <table border="0" cellpadding="5" cellspacing="0"> 
     <tr> 
      <td>Username:</td> 
      <td><input type="text" size="20" name="username" value=""></td> 
     </tr> 
     <tr> 
      <td>Password:</td> 
      <td><input type="password" size="20" name="password" value=""></td> 
     </tr> 
     <tr> 
      <td><input type="submit" name="axn" value=Login></td> 
     </tr> 
     </table> 
</form> 

यहाँ है मैं इसे कैसे .net से कार्य करें:

string userName = "user"; 
string password = "password"; 

string postData = "username=" + userName; 
      postData += ("&password=" + password); 
      postData += ("&axn=Login"); 

HttpWebRequest loginRequest = (HttpWebRequest) 
       WebRequest.Create("http://server.com/process-login.php"); 

//Added following answer begin 
CookieContainer CC = new CookieContainer(); 
loginRequest.CookieContainer = CC; 
//Added following answer end 

loginRequest.Method = "POST"; 
loginRequest.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*"; 
loginRequest.Headers.Add("Accept-Encoding: gzip,deflate"); 
loginRequest.Headers.Add("Accept-Language: en-us"); 
loginRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)"; 

loginRequest.ContentLength = postData.Length; 
loginRequest.ContentType = "application/x-www-form-urlencoded"; 

loginRequest.Referer = "http://server.com/login.php"; 
loginRequest.KeepAlive = true; 

//Also added 
loginRequest.AllowAutoRedirect = false; 

StreamWriter newStream = new StreamWriter(loginRequest.GetRequestStream()); 
newStream.Write(postData); 
newStream.Close(); 

//No cookie in the collection :-(

//Problem here, after this line loginRequest url's has changed 
//it's gone back to login.php 
HttpWebResponse responseLogin = (HttpWebResponse)loginRequest.GetResponse(); 


StreamReader stIn = new StreamReader(responseLogin.GetResponseStream()); 
string strResponse = stIn.ReadToEnd(); 
stIn.Close(); 

//strResponde contains the login page, still no cookie :-(

मैं अपने ब्राउज़र का उपयोग करने में लॉग इन और Fiddler इस के साथ की जाँच मैं ग्राहक के लिए क्या मिलता है:

POST http://server.com/process-login.php HTTP/1.1 
Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */* 
Referer: http://server.com/login.php 
Accept-Language: en-us 
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; WOW64; Trident/4.0; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729) 
Content-Type: application/x-www-form-urlencoded 
Accept-Encoding: gzip, deflate 
Host: server.com 
Content-Length: 45 
Connection: Keep-Alive 
Pragma: no-cache 

username=username&password=password&axn=Login 

और प्रतिक्रिया हेडर में मुझे मिलता है:

HTTP/1.1 302 Found 
Date: Thu, 20 May 2010 14:07:36 GMT 
Server: Apache/2.2.3 (Unix) 
Accept-Ranges: bytes 
X-Powered-By: PHP/5.2.0 
Set-Cookie: login=User%7C3142%7CUser+Inc.%7CAll+Orders+Discounted%7C; expires=Thu, 20-May-2010 22:07:36 GMT; domain=server.com 
Set-Cookie: username=deleted; expires=Wed, 20-May-2009 14:07:35 GMT; path=/; domain=server.com 
Set-Cookie: password=deleted; expires=Wed, 20-May-2009 14:07:35 GMT; path=/; domain=server.com 
Location: /index.php 
Content-Length: 0 
Keep-Alive: timeout=15, max=200 
Connection: Keep-Alive 
Content-Type: text/html 

कुकी !!!

मैं क्या गलत कर रहा हूं कि मुझे कुकी नहीं मिल सकती है?

अद्यतन: उत्तर के बाद कोड जोड़ना, अब मैं कुकी प्राप्त कर सकता हूं! मैं एक और प्रश्न खोलूंगा क्योंकि ऐसा लगता है कि मुझे अभी भी सुरक्षित पृष्ठ नहीं मिल सकते हैं ...

उत्तर

5

मैं आपको वेबक्यूस्ट पर कुकी कंटनर स्थापित करने के लिए नहीं देखता हूं। क्या आप इसे सेट करके पुनः प्रयास कर सकते हैं?

+0

आप सही हैं, मैंने कुकी कोंटेनर शुरू किया और अब यह वास्तव में भर जाता है! – Enriquev

1

चूंकि आपको 302 मिल रहा है, इसलिए वेबरेक्वेस्ट स्वचालित रूप से अगले यूआरएल का भी अनुरोध कर रहा है। आप इसे loginRequest.AllowAutoRedirect = false; सेट करके बंद कर सकते हैं। एक बार ऐसा करने के बाद, आपको कुकीज़ देखना चाहिए। Here's the documentation for AllowAutoRedrect.

+0

हैलो, मैंने loginRequest.AllowAutoRedirect = false जोड़ा; loginRequest.KeepAlive = true के बाद; अब पृष्ठ नहीं बदलेगा, लेकिन कुकी संग्रह अभी भी खाली है ... – Enriquev

संबंधित मुद्दे