Ios 对于简单POST请求,NSURLSession比NSURLConnection花费的时间更长
我有一个简单的post服务,它将json作为输入,并将json作为输出。当我使用NSURLConnection时,我会在500毫秒内得到响应。但如果我使用NSURLSession,则至少需要4-5秒才能响应相同的请求 同样,对于NSURLSession,DidCompleteWither错误始终处于触发状态,尽管没有错误且错误为零。但在NSURL连接失败的情况下,错误是tot发射 请告诉我NSURLSession有什么问题 使用NSURLConnection发布Ios 对于简单POST请求,NSURLSession比NSURLConnection花费的时间更长,ios,objective-c,Ios,Objective C,我有一个简单的post服务,它将json作为输入,并将json作为输出。当我使用NSURLConnection时,我会在500毫秒内得到响应。但如果我使用NSURLSession,则至少需要4-5秒才能响应相同的请求 同样,对于NSURLSession,DidCompleteWither错误始终处于触发状态,尽管没有错误且错误为零。但在NSURL连接失败的情况下,错误是tot发射 请告诉我NSURLSession有什么问题 使用NSURLConnection发布 -(void) postData
-(void) postData: (NSString *)requestBody toAddress: (NSString *)url{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)requestBody.length] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[requestBody dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse
{
data = [NSMutableData data];
[data setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)bytes
{
[data appendData:bytes];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Response http status code %ld", (long)[httpResponse statusCode]);
NSLog(@"response %@",[data getString]);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Error in service");
}
-(void) postData: (NSString *)requestBody toAddress: (NSString *)url{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)requestBody.length] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[requestBody dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration ephemeralSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];
NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:request];
[dataTask resume];
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler
{
httpResponse = (NSHTTPURLResponse*)response;
completionHandler(NSURLSessionResponseAllow);
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveData:(NSData *)data
{
NSLog(@"Response http status code %ld", (long)[httpResponse statusCode]);
NSLog(@"response %@",[data getString]);
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error
{
if (error != nil)
{
NSLog(@"Error in service call");
}
}
使用NSURLSession发布
-(void) postData: (NSString *)requestBody toAddress: (NSString *)url{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)requestBody.length] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[requestBody dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse
{
data = [NSMutableData data];
[data setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)bytes
{
[data appendData:bytes];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Response http status code %ld", (long)[httpResponse statusCode]);
NSLog(@"response %@",[data getString]);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Error in service");
}
-(void) postData: (NSString *)requestBody toAddress: (NSString *)url{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)requestBody.length] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[requestBody dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration ephemeralSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];
NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:request];
[dataTask resume];
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler
{
httpResponse = (NSHTTPURLResponse*)response;
completionHandler(NSURLSessionResponseAllow);
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveData:(NSData *)data
{
NSLog(@"Response http status code %ld", (long)[httpResponse statusCode]);
NSLog(@"response %@",[data getString]);
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error
{
if (error != nil)
{
NSLog(@"Error in service call");
}
}
要获得最佳教程,请执行以下操作: 另请参阅此问题: 尝试使用完成处理程序:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)requestBody.length] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[requestBody dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration ephemeralSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];
NSURLSessionDataTask *task = [defaultSession dataTaskWithURL:yourNSURL
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
// handle NSData
}];
[task resume];
古尔山,你找到解决办法了吗?