2013-05-10 98 views
1

我想使用的用戶和密碼錶單POST請求,訪問web應用程序在其下方,使用戶不必滿山遍野或按下登錄鍵來訪問他們的webapps。據我NSLogs,連接DidReceiveResponse,ConnectionDidReceiveData和ConnectionDidFinishLoading,但我看到在模擬器沒有結果; 我去web視圖,它顯示了空單,等待我輸入信息...不是我想要的。我想繞過這種形式。什麼是/可能會丟失或在這裏公然錯誤?iOS HTTP Post:請求成功連接;什麼也沒有發生

viewDidLoad中: ... USR,私服等之前*消息體中產生,等等

NSString *messageBody = [NSString stringWithFormat:@"UsernameFieldName=%@&PasswordFieldName=%@", usr, pW]; 

NSData *postData = [messageBody dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:appURL]; 
[theRequest setHTTPMethod:@"Post"]; 

[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
NSLog(@"%@", messageBody); 

[theRequest setHTTPBody:[messageBody dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]]; 

[theRequest addValue: messageBody forHTTPHeaderField:@"Content-Length"]; 
NSLog(@"%@", theRequest); 

[NSURLConnection connectionWithRequest: theRequest delegate:self]; 

連接方法:

- (void)connection:(NSURLConnection*)connection 
didReceiveResponse:(NSURLResponse*)response 
{ 
    NSLog(@"Connection DidReceiveResponse"); 

    webData = [[NSMutableData alloc] init]; 
} 

- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data 
{ 
    NSLog(@"ConnectionDidReceiveData"); 

    webData = [NSMutableData dataWithData:data]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection*)connection 
{ 
    NSLog(@"Connection Did Finish Loading"); 

    [HelpWebView loadData:webData 
       MIMEType:@"text/html" 
     textEncodingName:@"UTF-8" 
        baseURL:nil]; 
} 

以下是Sangony的貢獻,它接近一點,但還有其他要求得到t o表單下的web應用程序。

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{ 
    //KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"key" accessGroup:nil]; 
    //NSString *usr = [keychainItem objectForKey:(__bridge id)(kSecAttrAccount)]; 
    //NSString *pW = [keychainItem objectForKey:(__bridge id)(kSecValueData)]; 

    if ([challenge previousFailureCount] == 0) { 
    NSLog(@"received authentication challenge"); 
    NSURLCredential *newCredential = [NSURLCredential credentialWithUser:@"?username" 
                   password:@"?password" 
                  persistence:NSURLCredentialPersistenceForSession]; 
//  NSURLCredential *newCredential = [NSURLCredential credentialWithUser:[NSString stringWithFormat:@"%@", usr] 
//                 password:[NSString stringWithFormat:@"%@", pW] 
//                persistence:NSURLCredentialPersistenceForSession]; 
    NSLog(@"Credential created"); 
    [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge]; 
    NSLog(@"Responded to authentication challenge"); 
} 
    else { 
    NSLog(@"Authentication failure"); 
}} 

更新 我在這裏測試了上述方法: http://www.posttestserver.com/

的鍵和值是什麼,我需要爲我的形式。問題出在窗體/網站/服務器上。不知道該從哪裏出發。任何關於從哪裏開始尋找的建議都會很棒。

+0

現在我有同樣的問題!你有沒有想過解決你的問題的正確方法?謝謝你的時間! – wayway 2014-05-02 01:08:56

+0

它不久前,但我認爲事實證明,Web應用程序是問題,正如sangony下面提到的。 – Morkrom 2014-05-02 19:05:37

回答

1

看看這個代碼:

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{ 
if ([challenge previousFailureCount] == 0) { 
NSLog(@"received authentication challenge"); 
NSURLCredential *newCredential = [NSURLCredential credentialWithUser:@"?username" 
                  password:@"?password" 
                 persistence:NSURLCredentialPersistenceForSession]; 
NSLog(@"Credential created"); 
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge]; 
NSLog(@"Responded to authentication challenge");  
} 
else { 
NSLog(@"Authentication failure"); 
} 
+0

我認爲這是一些進展...這方法正確地記錄所有以「迴應了認證挑戰」 ......雖然沒有在我的模擬器發生的方式。是否還有其他工作必須完成以推送這些數據並在我的網頁視圖中瀏覽此表單? – Morkrom 2013-05-15 18:44:15

+1

如果您需要「響應身份驗證挑戰」,這意味着您的用戶名,密碼已被您的服務器接受。成功登錄後,在服務器/應用程序中接下來會發生什麼? – sangony 2013-05-15 18:50:47

+0

它應該加載一個webapp,它在成功的表單登錄之後發送。 – Morkrom 2013-05-15 18:54:52