网络连接相关类
2014-03-11 17:07
274 查看
NSURLConnection:支持ftp://,http://,https://,file:///(本地文件)连接
发送同步连接请求,该函数会阻塞,直到有响应返回+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error
异步连接请求,执行该函数后就立即开始连接;一般来说都在主线程中调用该函数;若在新线程中调用,则回调函数不会被执行,需通过scheduleInRunLoop使回调生效
+ (NSURLConnection *)connectionWithRequest:(NSURLRequest *)request delegate:(id)delegate
在其他线程中使异步连接的回调生效
- (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode
- (void)unscheduleFromRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode
一般情况下,我们使用NSURLConnection时,都使用如下的代码:
_connection = [[NSURLConnection alloc] initWithRequest:URLRequest delegate:self];
这段代码创建的connection运行在main runloop以及NSEventTrackingRunLoopMode下,而该模式在mouse-dragging
loops and other sorts of user interface tracking loops期间会阻止对该connection的代理通知事件,比如在UITableView滚动的时候,connection的代理方法就不会被通知。这不是我们想要的,如果想要在mouse-dragging loops and other sorts of user interface tracking loops期间我们的connection的代理方法仍然被调用,可用以下代码替换上面的代码:
_connection = [[NSURLConnection alloc] initWithRequest:URLRequest delegate:self startImmediately:NO];
[_connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[_connection start];
初始化一个请求,第一个构造函数等同于第二个构造函数的缓存策略参数为NSURLRequestUseProtocolCachePolicy,超时时间60秒
+ (id)requestWithURL:(NSURL *)theURL
+ (id)requestWithURL:(NSURL *)theURL cachePolicy:(NSURLRequestCachePolicy)cachePolicy timeoutInterval:(NSTimeInterval)timeoutInterval
NSMutableURLRequest:NSURLRequest子类,实际上该类就是提供了NSURLRequest所有属性的设置方法
常用设置函数:
- (void)setHTTPBody:(NSData *)data
- (void)setHTTPMethod:(NSString *)method
默认是GET
- (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field
- (void)addValue:(NSString *)value forHTTPHeaderField:(NSString *)field
与setValue的不同:若已存在一个键值,则附加新值到旧值的后面,以逗号分隔
NSURLResponse:
- (NSString *)suggestedFilename
返回网络数据需要保存的文件名
NSHTTPURLResponse:NSURLResponse的子类
- (NSDictionary *)allHeaderFields
获取HTTP服务器返回的头信息
- (NSInteger)statusCode 获取状态码
+ (NSString *)localizedStringForStatusCode:(NSInteger)statusCode
根据状态码获取本地化文本内容,比如状态码200表示无错误
NSURL:
- (NSString *)absoluteString
获取url的字符串
- (NSString *)query
获取url中问号后面跟的参数字符串
相关文章推荐
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误
- SQL无法连接到 (local)。在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL
- SQL Server 建立连接时出现与网络相关的或特定于实例的错误
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法訪问server。请验证实例名称是否正确而且 SQL Server 已配置为同意远程连接。
- vs中SQL Server建立连接时出现与网络相关的或特定的实例的错误。未找到或无法访问服务器
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。
- 在与SQL Server建立连接时出现与网络相关的或特定于实例的错误
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管
- Sql server在另一台服务器,在Visual Studio 中没问题,IIS中 提示“在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。。。。”
- Sql server在另一台服务器,在Visual Studio 中没问题,IIS中 提示“在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。。。。”
- ABP 执行 update-database报错 建立连接时出现与网络相关的或特定于实例的错误
- HTTP网络连接相关知识整理(一):http连接与Socket连接
- 【SQL Server学习笔记】2:在与SQL Server建立连接时出现与网络相关的或特定于实例的错误
- 与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误
- 遇到的问题-在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误