2011-11-15 12 views
-1

我正在開發基於視圖的應用程序。當我們登錄頁面時,我們點擊登錄按鈕它應該檢查輸入的值與Web服務器值,它應該顯示瓦利或invalid.I以這種方式已經wriiten代碼被執行成功我得到的結果是這樣如何使用Web服務器值驗證用戶名和密碼在我的電話應用程序

<!DOCTYPE html PUbLIC" -//W3C//DTD XHTML 1.0 Strict.... 

什麼,我需要改變在下面的代碼與服務器comapre values..can任何一個可以幫助我對此請.. 。

-(IBAction)buttonClick:(id)sender 
{ 

    NSString* username = nameInput.text; 
    NSString* pass = passInput.text; 

    if([nameInput.text isEqualToString:@"" ]|| [passInput.text isEqualToString:@""]) 
    { 

     greeting.text = @"Input Your Value"; 
     [nameInput resignFirstResponder]; 
     [passInput resignFirstResponder]; 
     return; 
    } 

    NSString *post = 
    [[NSString alloc] initWithFormat:@"uname=%@&pwd=%@",username,pass]; 

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

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

    NSURL *url = [NSURL URLWithString:@"https://108.16.210.28/Account/LogOn"]; 
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 
    [theRequest setHTTPMethod:@"POST"]; 
    [theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [theRequest setHTTPBody:postData]; 
    [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]]; 

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

    if(theConnection) 
    { 
     webData = [[NSMutableData data] retain]; 
    } 
    else 
    { 

    } 

    [nameInput resignFirstResponder]; 
    [passInput resignFirstResponder]; 
    nameInput.text = nil; 
    passInput.text = nil; 

} 

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [webData setLength: 0]; 
} 
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 


    [webData appendData:data]; 
} 
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 

    [connection release]; 
    [webData release]; 
} 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 

    NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; 
    NSLog(loginStatus); 
    greeting.text = loginStatus; 
    [loginStatus release]; 



    [connection release]; 
    [webData release]; 
} 


- (void)dealloc { 
    [super dealloc]; 
} 

@end 
+0

我們不是在這裏調試您的代碼。什麼,特別是,不工作?你有什麼嘗試? –

+0

代碼工作正常,但基於代碼我應該能夠連接網絡服務器,如果服務器的值與輸入的值匹配,它應該顯示消息有效無效,但我得到的應用程序的第一行HTML代碼 – Haritha

回答

0

使用statusCode看到登錄狀態。

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 

    if([response isKindOfClass:[NSHTTPURLResponse class]]) 
    { 
     NSHTTPURLResponse *theResponse = (NSHTTPURLResponse*)response; 
     NSInteger theStatusCode = [theResponse statusCode]; 
     } 
} 
+0

感謝您的reply.after添加狀態代碼也是我得到相同的以前輸出 – Haritha

+0

你在狀態代碼中看到什麼值? – nanjunda

+0

[[NSString alloc] initWithFormat:@「uname =%@&pwd =%@」,username,pass]; – Haritha

0

你必須解析數據。如果它是在XML數據的形式,那麼你必須解析元件(如有效)在didfoundcharacters.If它有效,則使像BOOL確認變量= NO;在didfoundcharacters.if中修改此(確認),然後進一步訪問

+0

我如何使用..你可以舉個小例子與描述 – Haritha

相關問題