2013-08-06 109 views
0

我試圖與網站進行身份驗證用戶名和密碼...ASIHttpRequest登錄身份驗證問題

我使用ASIHttpRequest這樣的:

- (IBAction)fetchTopSecretInformation:(id)sender{ 
    Ustr = User.text; //Ustr and Pstr are strings 
    Pstr = Pass.text; 

    NSURL *url1 = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.MySite/login/"]]; 
    ASIFormDataRequest *request1 = [ASIFormDataRequest requestWithURL:url1]; 
    [request1 setUseKeychainPersistence:YES]; 
    request1.delegate = self; 
    request1.shouldPresentCredentialsBeforeChallenge = YES; 

    [request1 setRequestMethod:@"POST"]; 
    [request1 setPostValue:User.text forKey:@"username"]; 
    [request1 setPostValue:Pass.text forKey:@"passwd"]; 
    [request1 setTimeOutSeconds:30]; 
    NSLog(@"Useer :%@",User.text); 

    [request1 setDidFailSelector:@selector(topSecretFetchFailed:)]; 
    [request1 setDidFinishSelector:@selector(topSecretFetchComplete:)]; 
    [request1 startAsynchronous]; 
} 

- (IBAction)topSecretFetchFailed:(ASIHTTPRequest *)theRequest{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Error sending request to the server" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
} 

- (IBAction)topSecretFetchComplete:(ASIHTTPRequest *)theRequest{ 
    int statusCode = [theRequest responseStatusCode]; 
    NSString *statusMessage = [theRequest responseStatusMessage]; 

    NSString *strResponse = [[theRequest responseString] stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
    NSString *strFinal = [self flattenHTML:strResponse]; 
    NSLog(@"Response :%@",strFinal); 
    NSLog(@"StatusCode: %d", statusCode); 
    NSLog(@"StatusMessage: %@", statusMessage); 
} 

我得到像這樣的NSLog響應:

jQuery(function($) { $(".main").addClass("show-bg"); }); 

JavaScript seems to be disabled in your browser. 
You must have JavaScript enabled in your browser to utilize the functionality of this website.     

This is a demo store. Any orders placed through this store will not be honored or fulfilled. 

         +995 91 190601 

      მოგესალმებით ელექტრონული წიგნების მაღაზიაში 

         READERwill.com 

    კალათა 



         კალათა GEL 0,00 
          სავაჭრო კალათა ცარიელია. 

ჩემი ბიბლიოთეკა 
სურვილები 
რეგისტრაცია 

. 
. 
. 
. 
. 

和狀態代碼和狀態消息是

的StatusCode:200
StatusMessage:HTTP/1.1 200 OK

這是什麼反應,我該如何使用這個認證

在我的web服務,如果用戶名和密碼是正確的「成功」消息顯示和其他故障信息顯示...

回答

0
- (IBAction)topSecretFetchComplete:(ASIHTTPRequest *)theRequest 
{ 
    NSString *statusMessage = [theRequest responseData]; 
    NSLog(@"StatusCode: %d", statusCode); 
    //at this methods you get authuntication 
    //write code here 
} 
+0

我得到了statusCode:200和statusMessage:Sattus代碼:<3c21444f 43545950 45206874 6d6c2050 55424c49 4320222d 2f2f5733 432f2f44像這樣 – Denny

+0

'topSecretFetchComplete'方法在你的認證成功時調用。所以你在這裏寫代碼重定向到認證後出現的其他視圖 –

+0

如果我輸入了錯誤的密碼,那麼也調用topSecretFetchComplete?爲什麼? – Denny

1

如果您正在爲新項目工作,那麼你不應該使用ASIHTTP框架。檢查這個鏈接中給出的指令http://allseeing-i.com/ASIHTTPRequest/這是最後更新於2011年。

只是試圖在NSLog中只顯示「strResponse」。我認爲它應該正確顯示響應。

0

HTTP 200響應代碼是來自Web服務器的正常響應。如果您使用表單身份驗證,就像您看到的那樣,我會檢查響應中設置的cookie,因爲我希望會話cookie在成功登錄時返回。 IIRC,ASIHTTPRequest將默認存儲cookie,並自動發送後續請求。

可能都無所謂什麼樣的內容,你在響應收到只要設置cookie和其他錯誤條件不發生(如HTTP 401或其他錯誤),但這是高度依賴於你情況。您可能需要解析錯誤的響應,此時,服務器似乎在嗅探請求的代理字符串,並檢測到您的客戶端不支持JavaScript,或者期望類似以JSON方式提交的表單格式作爲AJAX調用的一部分,並假定該腳本失敗,說明JavaScript在瀏覽器中沒有正常執行(它不是)。

您現在正在將名爲usernamepasswd的字段提交到網址https://www.MySite/login/

這似乎是網頁的網址,而不是網絡服務。如果你在這個網址查看登錄頁面上的登錄表單,它有憑據兩個字段,以及他們所使用的名稱或ID usernamepasswd,如:

<form action="https://www.MySite/login/" method="POST"> 
    <input name="username" type="text" /> 
    <input name="passwd" type="password" /> 
</form> 

或者:

<form action="https://www.MySite/login/" method="POST"> 
    <input id="username" type="text" /> 
    <input id="passwd" type="password" /> 
</form> 

否則,如果您使用的是通過SOAP或JSON進行通信的Web服務,則需要使用該格式格式化認證請求,而不是URL編碼的表單數據。

+0

好吧,現在我正在使用網頁登錄頁面,但如果我輸入錯誤的密碼,那麼也csll topSecretFetchComplete爲什麼? – Denny

+0

@ Denny - 由於請求正確完成,服務器剛剛返回了一個表明密碼不正確的響應。這本身並不是一個錯誤。如果發送/接收請求/響應時發生錯誤,ASIHTTPRequest框架只會調用失敗方法,而不是在您的應用程序發出響應時指示登錄失敗。解決指示登錄失敗的錯誤代碼/消息的響應由您決定。在這種情況下,最好將請求發送到一個Web服務,該Web服務將形成可靠地指示以預期格式登錄失敗的響應。 – nekno