2012-02-05 106 views
2

我想使用NSMutableURLRequest自動登錄到ASP.NET網站。我正在發送所有可以想到的帖子參數,並且與其他網站的效果很好。 (如基於WordPress的)。但我無法讓它爲ASP.net網站工作。有人可以向我解釋我該如何解決這個問題?Objective C - ASP.NET身份驗證

NSString *post = @"__LASTFOCUS=&__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwUJNjM4NDU2MzY5ZGT%2F7rz73weImm5JbYQQ4q2lRY3HUw%3D%3D&__EVENTVALIDATION=%2FwEWBQK9gI6LAwKY4eTfDQKmw8%2B%2FAQLerrfAAwKQxuCGAT0xfZqPEPwFCfa5fbrvTZXDSnbY&ctl00%24SideBarContent%24UserName=demo&ctl00%24SideBarContent%24Password=demo&ctl00%24SideBarContent%24LoginButton=Login&ctl00%24SideBarContent%24UserTimeZoneCrawler%24offset=02%3A00%2C0"; 
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

NSMutableURLRequest *postRequest = [[NSMutableURLRequest alloc] init]; 
[postRequest setURL:[NSURL URLWithString:@"http://asp.net/website/logon.aspx"]]; 
[postRequest setHTTPMethod:@"POST"]; 
[postRequest setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[postRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[postRequest setHTTPBody:postData]; 

[postRequest setValue:@"loginCookie=UserName=demo; SessionContext=ydnwyyz5n50mt2zjn3yiarq0" forHTTPHeaderField:@"Cookie"]; 

問候, EZFrag

+0

檢查[NSURLConnection的和基本HTTP認證] [1] [1]:http://stackoverflow.com/questions/1973325/nsurlconnection-and-basic- http認證 – 2013-10-14 06:12:37

回答

0

您可能必須與您的網址通過您的用戶名/密碼。

[postRequest setURl:[NSURL URLWithString:@"http://<username>:<password>@asp.net/website/login.aspx"]]; 
+0

我相信,該方法用於「瀏覽器認證」。無論如何,我嘗試過,但沒有結果。 – EZFrag 2012-02-14 12:45:23

0

我最終讓用戶每次都手動輸入用戶名和密碼。儘管如此,我仍然接受任何建議。

問候, EZFrag