SORU
29 Aralık 2009, Salı


İOS NSURLConnection ve Temel HTTP kimlik Doğrulaması

Authentication Temel GET HTTP request Bir başlangıç çağırmak istiyorum. Bu isteği sunucuya gönderilir ilk kez olacak ve ben zaten yetkilendirme sunucusundan bir meydan okuma için gerek yok yani username & password var.

İlk soru:

  1. NSURLConnection Temel Auth yapmak için zaman uyumlu olarak ayarlanmış olması gerekiyor mu? Bu cevaba göre post, eğer uyumsuz yolu seçerseniz Basic Auth yapamazsın gibi görünüyor.

  2. Herkes bir meydan okuma için gerek kalmadan GET request Bir yanıt üzerinde Temel Auth gösteren herhangi bir örnek kod biliyor musun? Apple's documentation bir örnek ama Sunucu Kimlik sorma isteği verildikten sonra istemciye gösterir.

Biraz SDK ağ kısmı yeniyim ve bu çalışma için kullanmam gerektiğinden emin değilim. (NSURLCredential sınıf görüyorum ama istemci sunucudan yetkili bir kaynak için talep ettikten sonra sadece NSURLAuthenticationChallenge ile kullanılır gibi görünüyor).

CEVAP
25 Temmuz 2011, PAZARTESİ


Hatta soru cevap, dış libs gerektirmeyen çözüm sunmak istiyorum, başka bir konu buldum:

// Setup NSURLConnection
NSURL *URL = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:URL
                                         cachePolicy:NSURLRequestUseProtocolCachePolicy
                                     timeoutInterval:30.0];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
[connection release];

// NSURLConnection Delegates
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    if ([challenge previousFailureCount] == 0) {
        NSLog(@"received authentication challenge");
        NSURLCredential *newCredential = [NSURLCredential credentialWithUser:@"USER"
                                                                    password:@"PASSWORD"
                                                                 persistence:NSURLCredentialPersistenceForSession];
        NSLog(@"credential created");
        [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
        NSLog(@"responded to authentication challenge");    
    }
    else {
        NSLog(@"previous authentication failure");
    }
}

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

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    ...
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    ...
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    ...
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • AmazonWireless

    AmazonWirele

    8 EYLÜL 2010
  • Dave Wallace

    Dave Wallace

    27 Kasım 2007
  • Chaîne de TheMoustic

    Chaîne de T

    5 Kasım 2006