[英]NSURLConnection synchronous request on https
誰能告訴我如何與https服務器進行同步調用? 我可以使用以下委托方法在https服務器上執行異步請求。
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
和
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
但我需要做同步。
//編碼請求
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];
+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error
在NSUrlConnection
應該可以正常使用https。
如果您想提供憑據,則需要成為URL的一部分:( https://username:password@domain.tld/api/user.json
)。
無法提供NSURLConnection
委托,因此如果您需要一些非標准的身份驗證處理,則需要異步執行。
這就是我做到的方式:而不是
[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())我希望它可以幫助其他人在未來。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.