iOS使用NSURLConnection发送同步和异步HTTP Request
2014-01-02 14:09
351 查看
1. 同步发送
- (NSString *)sendRequestSync
{
// 初始化请求, 这里是变长的, 方便扩展
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
// 设置
[request setURL:[NSURL URLWithString:urlStr]];
[request setHTTPMethod:@"POST"];
[request setValue:host forHTTPHeaderField:@"Host"];
NSString *contentLength = [NSString stringWithFormat:@"%d", [content length]];
[request setValue:contentLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:content];
// 发送同步请求, data就是返回的数据
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nilerror:&error];
if (data == nil) {
NSLog(@"send request failed: %@", error);
return nil;
}
NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"response: %@", response);
return response;
}
2.异步发送
1) 使用delegate的方式:
- (void)sendRequestAsync
{
// 初始化请求
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
// 设置
[request setURL:[NSURL URLWithString:urlStr]];
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy]; // 设置缓存策略
[request setTimeoutInterval:5.0]; // 设置超时
//......
receivedData = [[NSMutableData alloc] initData: nil];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection == nil) {
// 创建失败
return;
}
}
异步发送使用代理的方式, 需要实现以下delegate接口:
// 收到回应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"receive the response");
// 注意这里将NSURLResponse对象转换成NSHTTPURLResponse对象才能去
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
if ([response respondsToSelector:@selector(allHeaderFields)]) {
NSDictionary *dictionary = [httpResponse allHeaderFields];
NSLog(@"allHeaderFields: %@",dictionary);
}
[receivedData setLength:0];
}
// 接收数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"get some data");
[receivedData appendData:data];
}
// 数据接收完毕
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *results = [[NSString alloc]
initWithBytes:[receivedData bytes]
length:[receivedData length]
encoding:NSUTF8StringEncoding];
NSLog(@"connectionDidFinishLoading: %@",results);
}
// 返回错误
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Connection failed: %@", error);
}
2) iOS 5.0版本新增异步发送接口:
+ (void)sendAsynchronousRequest:(NSURLRequest *)request
queue:(NSOperationQueue*) queue
completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*)) handlerNS_***AILABLE(10_7, 5_0);
转自:http://www.cnblogs.com/zhwl/archive/2013/01/25/2876473.html
- (NSString *)sendRequestSync
{
// 初始化请求, 这里是变长的, 方便扩展
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
// 设置
[request setURL:[NSURL URLWithString:urlStr]];
[request setHTTPMethod:@"POST"];
[request setValue:host forHTTPHeaderField:@"Host"];
NSString *contentLength = [NSString stringWithFormat:@"%d", [content length]];
[request setValue:contentLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:content];
// 发送同步请求, data就是返回的数据
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nilerror:&error];
if (data == nil) {
NSLog(@"send request failed: %@", error);
return nil;
}
NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"response: %@", response);
return response;
}
2.异步发送
1) 使用delegate的方式:
- (void)sendRequestAsync
{
// 初始化请求
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
// 设置
[request setURL:[NSURL URLWithString:urlStr]];
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy]; // 设置缓存策略
[request setTimeoutInterval:5.0]; // 设置超时
//......
receivedData = [[NSMutableData alloc] initData: nil];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection == nil) {
// 创建失败
return;
}
}
异步发送使用代理的方式, 需要实现以下delegate接口:
// 收到回应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"receive the response");
// 注意这里将NSURLResponse对象转换成NSHTTPURLResponse对象才能去
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
if ([response respondsToSelector:@selector(allHeaderFields)]) {
NSDictionary *dictionary = [httpResponse allHeaderFields];
NSLog(@"allHeaderFields: %@",dictionary);
}
[receivedData setLength:0];
}
// 接收数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"get some data");
[receivedData appendData:data];
}
// 数据接收完毕
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *results = [[NSString alloc]
initWithBytes:[receivedData bytes]
length:[receivedData length]
encoding:NSUTF8StringEncoding];
NSLog(@"connectionDidFinishLoading: %@",results);
}
// 返回错误
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Connection failed: %@", error);
}
2) iOS 5.0版本新增异步发送接口:
+ (void)sendAsynchronousRequest:(NSURLRequest *)request
queue:(NSOperationQueue*) queue
completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*)) handlerNS_***AILABLE(10_7, 5_0);
转自:http://www.cnblogs.com/zhwl/archive/2013/01/25/2876473.html
相关文章推荐
- iOS使用NSURLConnection发送同步和异步HTTP Request
- iOS使用NSURLConnection发送同步和异步HTTP Request
- iOS使用NSURLConnection发送同步和异步HTTP Request
- iOS使用NSURLConnection发送同步和异步HTTP Request
- iOS使用NSURLConnection发送同步和异步HTTP Request
- iOS总结:ASIHttpRequest类库发送请求(同步请求和异步请求)
- iOS中使用NSURLConnection处理HTTP同步与异步请求
- iOS 网络开发NSURLConnection——使用block回调方法发送异步请求
- IOS 学习笔记 —— ASIHTTPRequest 之 发送异步请求
- iOS 网络开发NSURLConnection——使用代理方法发送异步请求
- iOS网络篇---使用NSConnection 实现post和get发送和接收(同步和异步模式)
- iOS 使用ASIHTTPRequest 发送Webservice请求
- [IOS_HTTP]NSURLConnection同步与异步请求
- ios 使用ASIHTTPRequest异步读取远程txt文件
- ASIHTTPRequest框架使用(1)--发送同步请求
- ASIHTTPRequest框架使用(2)--发送异步请求
- ASIHTTPRequest的使用——同步或异步请求
- IOS网络篇10之NSURLConnection的使用(同步异步下载getpost)
- 使用xmlHttprequest 发送异步请求(Ajax核心对象)
- ios中的ASIHTTPRequest的同步请求和异步请求