2011-01-06 79 views
19

任何人都可以告訴我如何與https服務器進行同步呼叫嗎?我能夠使用下面的委託方法在https服務器上執行異步請求。https上的NSURLConnection同步請求

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace 

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 

,但我需要做同步。

回答

13
+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error 

in NSUrlConnection應該可以正常使用https。

如果您想提供憑據,他們需要成爲網址的一部分:(https://username:[email protected]/api/user.json)。

無法提供NSURLConnection委託,因此如果您需要一些非標準身份驗證處理,則需要異步執行。

+0

這個傢伙需要一個實例相關的函數調用,以便提供委託來響應https挑戰 – LolaRun 2013-07-01 09:48:28

+0

謝謝:)它非常簡單。 – 2013-07-03 11:03:48

24

//編碼請求

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

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

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL:[NSURL URLWithString:requestUrlString]]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:postData]; 
NSURLResponse* response; 
NSError* error = nil; 

//Capturing server response 
NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
+2

使用NSUTF8StringEncoding應該是首選的,如果有數據也具有國際字符支持,例如:德語,意大利語等。否則上述方法絕對是完美的.. – 2012-10-09 10:32:11

6

這就是我做的:的 代替

[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error] 

我提出基於同樣的方法例如,在包含類,因爲我們將需要一個代表。不要讓它成爲單例,所以每個連接都有其獨立變量,因爲如果我們不這樣做,並且在另一個連接完成之前碰巧調用了兩個連接,那麼接收到的數據和循環的處理將不可挽回地交織在一起。

[[ClassNameHere new] sendSynchronousRequest:request returningResponse:&response error:&error] 

這樣我可以創建一個NSURL連接,並處理它(以同步的方式,我們將看到如何),所以我沒有改變任何以前編寫的代碼的。

- (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse *__strong*)response error:(NSError *__strong*)error 
{ 
    _finishedLoading=NO; 
    _receivedData=[NSMutableData new]; 
    _error=error; 
    _response=response; 

    NSURLConnection*con=[NSURLConnection connectionWithRequest:request delegate:self]; 
    [con start]; 
    CFRunLoopRun(); 

    return _receivedData; 
} 


- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { 
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{ 
    //handle the challenge 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    *_response=response; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [_receivedData appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    *_error=error; 
    CFRunLoopStop(CFRunLoopGetCurrent()); 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    CFRunLoopStop(CFRunLoopGetCurrent()); 
} 

訣竅是在CFRunLoopRun()和CFRunLoopStop(CFRunLoopGetCurrent()) 我希望它可以幫助別人在未來的危機別的。