https上的NSURLConnection同步请求
任何人都可以告诉我如何与https服务器进行同步呼叫吗?我能够使用下面的委托方法在https服务器上执行异步请求。https上的NSURLConnection同步请求
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
和
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
,但我需要做同步。
+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error
in NSUrlConnection
应该可以正常使用https。
如果您想提供凭据,他们需要成为网址的一部分:(https://username:[email protected]/api/user.json
)。
无法提供NSURLConnection
委托,因此如果您需要一些非标准身份验证处理,则需要异步执行。
//编码请求
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];
使用NSUTF8StringEncoding应该是首选的,如果有数据也具有国际字符支持,例如:德语,意大利语等。否则上述方法绝对是完美的.. – 2012-10-09 10:32:11
这就是我做的:的 代替
[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()) 我希望它可以帮助别人在未来的危机别的。
这个家伙需要一个实例相关的函数调用,以便提供委托来响应https挑战 – LolaRun 2013-07-01 09:48:28
谢谢:)它非常简单。 – 2013-07-03 11:03:48