NSURLRequest详解
2015-12-23 14:14
441 查看
http://blog.csdn.net/paulluo0739/article/details/6843312
URLRequest 的一个实例
[cpp] view
plaincopy
// Create the request.
//所构建的NSURLRequest具有一个依赖于缓存响应的特定策略,cachePolicy取得策略,timeoutInterval取得超时值
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
receivedData = [[NSMutableData data] retain];
} else {
// Inform the user that the connection failed.
}
其中:
NSURLRequest默认的cache policy是NSURLRequestUseProtocolCachePolicy,
是最能保持一致性的协议。
NSURLRequestReloadIgnoringCacheData 忽略缓存直接从原始地址下载
NSURLRequestReturnCacheDataElseLoad 只有在cache中不存在data时才从原始地址下载
NSURLRequestReturnCacheDataDontLoad 允许app确定是否要返回cache数据,如果使用这种协议当本地不存在response的时候,创建NSURLConnection
or NSURLDownload实例时将会马上返回nil;这类似于离线模式,没有建立网络连接;
你只需要实现以下delegate方法来处理数据响应
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSHTTPURLResponse*)response
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
NSURLConnect还提供了一个方便的类方法(class method) : sendSynchronousRequest:returningResponse:error:可用来 同步地加载一个URL请求
+ (NSData *)sendSynchronousRequest: (NSURLRequest *)request returningResponse: (NSURLResponse **)response error: (NSError **)error
request 要装载的URL请求. 这个request 对象 作为初始化进程的一部分,被深度复制(deep-copied). 在这个方法返回之后, 再修改request, 将不会影响用在装载的过程中的request
reponse 输出参数, 由服务器返回的URL响应
error 输出参数, 如果在处理请求的过程中发生错误,就会使用. 无错误,就为NULL
一个实现异步get请求的例子:
[cpp] view
plaincopy
NSString *url = [NSString stringWithFormat:@"http://localhost/chat/messages.php?past=%ld&t=%ld",
lastId, time(0) ];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"GET"];
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn)
{
receivedData = [[NSMutableData data] retain];
}
else
{
}
- (void)timerCallback {
//[timer release];
[self getNewMessages];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if (chatParser)
[chatParser release];
if ( messages == nil )
messages = [[NSMutableArray alloc] init];
chatParser = [[NSXMLParser alloc] initWithData:receivedData];
[chatParser setDelegate:self];//set the delegate
[chatParser parse];//start parse
[receivedData release];
[messageList reloadData];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
[self methodSignatureForSelector: @selector(timerCallback)]];
[invocation setTarget:self];
[invocation setSelector:@selector(timerCallback)];
//timer = [NSTimer scheduledTimerWithTimeInterval:5.0 invocation:invocation repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:5.0 invocation:invocation repeats:NO];//if set yes,then very 5 seconds updata the table
}
一个实现同步Get请求的例子:
[cpp] view
plaincopy
// 初始化请求
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
// 设置URL
[request setURL:[NSURL URLWithString:urlStr]];
// 设置HTTP方法
[request setHTTPMethod:@"GET"];
// 发 送同步请求, 这里得returnData就是返回得数据了
NSData *returnData = [NSURLConnection sendSynchronousRequest:request
returningResponse:nil error:nil];
// 释放对象
[request release];
更多详细信息请参见苹果开发文档:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
URLRequest 的一个实例
[cpp] view
plaincopy
// Create the request.
//所构建的NSURLRequest具有一个依赖于缓存响应的特定策略,cachePolicy取得策略,timeoutInterval取得超时值
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
receivedData = [[NSMutableData data] retain];
} else {
// Inform the user that the connection failed.
}
其中:
NSURLRequest默认的cache policy是NSURLRequestUseProtocolCachePolicy,
是最能保持一致性的协议。
NSURLRequestReloadIgnoringCacheData 忽略缓存直接从原始地址下载
NSURLRequestReturnCacheDataElseLoad 只有在cache中不存在data时才从原始地址下载
NSURLRequestReturnCacheDataDontLoad 允许app确定是否要返回cache数据,如果使用这种协议当本地不存在response的时候,创建NSURLConnection
or NSURLDownload实例时将会马上返回nil;这类似于离线模式,没有建立网络连接;
你只需要实现以下delegate方法来处理数据响应
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSHTTPURLResponse*)response
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
NSURLConnect还提供了一个方便的类方法(class method) : sendSynchronousRequest:returningResponse:error:可用来 同步地加载一个URL请求
+ (NSData *)sendSynchronousRequest: (NSURLRequest *)request returningResponse: (NSURLResponse **)response error: (NSError **)error
request 要装载的URL请求. 这个request 对象 作为初始化进程的一部分,被深度复制(deep-copied). 在这个方法返回之后, 再修改request, 将不会影响用在装载的过程中的request
reponse 输出参数, 由服务器返回的URL响应
error 输出参数, 如果在处理请求的过程中发生错误,就会使用. 无错误,就为NULL
一个实现异步get请求的例子:
[cpp] view
plaincopy
NSString *url = [NSString stringWithFormat:@"http://localhost/chat/messages.php?past=%ld&t=%ld",
lastId, time(0) ];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"GET"];
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn)
{
receivedData = [[NSMutableData data] retain];
}
else
{
}
- (void)timerCallback {
//[timer release];
[self getNewMessages];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if (chatParser)
[chatParser release];
if ( messages == nil )
messages = [[NSMutableArray alloc] init];
chatParser = [[NSXMLParser alloc] initWithData:receivedData];
[chatParser setDelegate:self];//set the delegate
[chatParser parse];//start parse
[receivedData release];
[messageList reloadData];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
[self methodSignatureForSelector: @selector(timerCallback)]];
[invocation setTarget:self];
[invocation setSelector:@selector(timerCallback)];
//timer = [NSTimer scheduledTimerWithTimeInterval:5.0 invocation:invocation repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:5.0 invocation:invocation repeats:NO];//if set yes,then very 5 seconds updata the table
}
一个实现同步Get请求的例子:
[cpp] view
plaincopy
// 初始化请求
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
// 设置URL
[request setURL:[NSURL URLWithString:urlStr]];
// 设置HTTP方法
[request setHTTPMethod:@"GET"];
// 发 送同步请求, 这里得returnData就是返回得数据了
NSData *returnData = [NSURLConnection sendSynchronousRequest:request
returningResponse:nil error:nil];
// 释放对象
[request release];
更多详细信息请参见苹果开发文档:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
相关文章推荐
- Xcode7 自带UI单元测试详解 带demo UITests
- 关于for(;;)和while(1)/while(true)区别
- MultipartRequest实现文件上传,并修改上传文件名称
- iOS预览PDF文档 UIDocumentInteractionController
- 【AC自动机】[UESTC 554][USACO 2012]Video Game Combos
- Android导入Liblary包后报Value = 2系列错误
- VS2013编译Duilib界面库,“找不到Riched20.lib”的问题
- request.getAttribute() 和 request.getParameter() 有何区别
- Chrome 43+浏览器 Cookies encrypted_value解密脚本
- 20.给UITextField添加一些小动画, 增强UI界面的趣味
- iOS UI-Lable标签、NStimer定时器和RunLoop超级死循环
- 19.制作自动切换滚动的图片轮播
- IOS学习笔记之UISwitch
- iOS UISearchDisplayController学习笔记
- 【ZSTU4215 2015年12月浙理工校赛 F】【Dijkstra最短路】Landlocked 到达海洋最少跨越国家数
- UI布局分析工具-视图工具(Hierarchy Viewer)
- 仿支付宝密码输入界面+随机键盘
- UITextField键盘类型
- UITextField键盘类型
- NGUI研究院之3D模型坐标转2D屏幕坐标-血条