2013-03-12 60 views
0

我有一個附帶密碼的webservices url。當我通過提供正確的密碼運行應用程序時,應用程序正在運行,同時當我運行具有錯誤憑據的應用程序時。該應用程序正在運行,我沒有一個想法,以擺脫這個問題了,我的代碼是在這裏不用..使用NSURL方法登錄憑證問題

 NSString *post =[[NSString alloc] initWithFormat:@"password=%@",[password text]]; 
     NSLog(@"PostData: %@",post); 
     NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://myexample.com/Accountservice/Secure/ValidateAccess?password=abcd&type=1"]]; 

     NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
     NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 
     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
     [request setURL:url]; 
     [request setHTTPMethod:@"POST"]; 
     [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
     [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
     [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
     [request setHTTPBody:postData]; 

的NSURLRequest * request1 = [[的NSURLRequest頁頭] initWithURL:[NSURL URLWithString:交] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];

 NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request1 delegate:self startImmediately:YES]; 
     if(!connection){ 
      NSLog(@"connection failed"); 
     } 
     else{ 
      NSLog(@"connection succeeded"); 
     } 

如果我給密碼連接成功顯示'abcd'。如果我給wrng密碼連接成功顯示。你能幫助解決這個問題嗎?如果我給wrng密碼需要顯示警報。提前致謝。

+0

accesscode =「ABCD」和輸入=「1」 – iPatel 2013-03-12 05:08:49

+0

它們只是附加到安全問題該網址,因爲它是網絡服務的URL ... – 2013-03-12 05:12:30

+0

這裏發佈不做任何事情,如果你傳遞的價值在網址! – 2013-03-12 05:24:13

回答

0

該問題可能與您使用

  1. 火的網址在瀏覽器的URL,看看它returnes
  2. 嘗試添加網址參數用單引號accesscode =「ABCD」

通過這種方式傳遞證書不是一個好習慣。 你可以使用POST方法與一些有效的加密,以防止可能發生的

SOLUTION 試試這個

NSString *[email protected]"http://myexample.com/Accountservice/Secure/ValidateAccess?"; 
    self.responseData = [NSMutableData data]; 
    NSString *post =[NSString stringWithFormat:@"username=%@&password=%@",userNameTextField.text,passwordTextField.text]; 
    NSData *postData=[post dataUsingEncoding:NSUTF8StringEncoding]; 
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
    [request setURL:[NSURL URLWithString:urlString]]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody:postData]; 
    NSURLConnection *connection0= [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    [connection0 start]; 
+0

我在發佈方法時遇到問題,能否檢查一次我的代碼...? – 2013-03-12 05:44:36

+0

@sudheerchekuri:是的這就是我說你使用的是後期的方法,但你在那裏發佈?沒有!!!請通過示例我提供你可以看到數據被添加爲Http身體,並將獲得發佈 – 2013-03-12 05:46:17

+0

我將發送代碼通過粘貼bin.Can你檢查一次,因爲它是我的迫切要求。 – 2013-03-12 05:56:05

1

這裏這種控制結構的問題是:

if(!connection){ 
    NSLog(@"connection failed"); 
} 
else { 
    NSLog(@"connection succeeded"); 
} 

這不檢查連接不良,此檢查連接物體是否等於0(無)。很明顯,在上面的行中初始化它後,else語句會一直記錄下來,給你錯誤的印象,說明你的連接已經成功。您應該成爲連接的delegate,並對相應的委託方法進行響應。