iOS网络-01-NSURLRequest与NSURLConnection
NSURLRequest
-
NSURLRequest封装了一次网络请求所需要的数据,主要封装了以下信息:
- 请求路径(URL)
- 请求方法(GET或POST)
- 请求头
- 请求体
- 超时参数
-
NSURLRequest与其子类NSMutableURLRequest
- NSURLRequest的所有的请求信息拼接在请求路径(URL)的后面
- NSMutableURLRequest的请求路径与其他的请求信息分开,其他请求信息通过对应的Key对请求对象进行设置
- NSURLRequest通常用于GET请求
- NSMutableURLRequest通常用于POST请求
-
NSURLRequest封装一次网络请求的的步骤
//1.创建请求路径 NSString *strURL = [NSString stringWithFormat:@"(此处为URL)/login?username=%@&pwd=%@", @"用户名", @"密码"]; NSURL *url = [NSURL URLWithString:]; //2.根据请求路径封装请求 NSURLRequest *request = [NSURLRequest requestWithURL:url];
-
NSMutableURLRequest封装一次网络请求的的步骤
//1.创建请求路径 NSURL *url = [NSURL URLWithString:@"(此处为URL)/login"]; //2.创建请求 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; //3.设置请求方法 request.HTTPMethod = @"POST"; //4.设置请求参数 request.HTTPBody = [@"username="用户名"&pwd="密码" dataUsingEncoding:NSUTF8StringEncoding]; //5.设置超时 request.timeoutInterval = 5;
NSURLConnection
-
NSURLConnection发送请求的步骤
- 创建请求路径(NSURL)
- 将请求路径封装成请求对象(NSURLRequest),设置其他请求参数
- 使用NSURLConnection发送同步/异步请求
-
NSURLConnection的代理
-
NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error /** *遇到错误的时候调用,请求终止 */
-
NSURLConnectionDataDelegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response /** *接收到服务器响应的时候调用 *response的中包含了服务器的响应信息,比较有价值是此次请求的数据的总长度 */ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data /** *接收到服务器的数据的时候调用,若数据较多会多次调用 *通常在该方法中对服务器返回的数据进行存储 *也可以在该方法中计算下载进度 */ - (void)connectionDidFinishLoading:(NSURLConnection *)connection /** *数据加载完毕的时候调用 */
-
NSURLConnectionDownloadDelegate
- (void)connection:(NSURLConnection *)connection didWriteData:(long long)bytesWritten totalBytesWritten:(long long)totalBytesWritten expectedTotalBytes:(long long) expectedTotalBytes /** *每次向沙盒中写文件都会调用该方法 */ - (void)connectionDidResumeDownloading:(NSURLConnection *)connection totalBytesWritten:(long long)totalBytesWritten expectedTotalBytes:(long long) expectedTotalBytes /** *该方法是支持断点下载的核心 */ - (void)connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *) destinationURL /** *由于:下载的文件保存在tmp文件夹中,该文件夹中的数据会被系统定时删除 *所以该方法必须实现,用于将改变数据的存储位置 */
-
-
NSURLConnection的请求方式
-
同步请求(线程会被阻塞)
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; /** *data:服务器返回的数据,即请求的数据 *request:请求请求对象 *response:服务器的响应数据 *error:错误信息 */
-
异步请求
//方法一(block) [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { /** *请求完成回调的block,参数的含义与铜鼓请求相同 */ }]; //方法二(代理) [NSURLConnection connectionWithRequest:request delegate:self] /** *自动发送请求 */ NSURLConnection *connect = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO]; /** *需要手动发送请求 */
-
URL中的中文处理
-
URL中的中文通要进行处理,通常使用UTF-8编码
//进行如下转码 [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
分类:
iOS
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 本地部署 DeepSeek:小白也能轻松搞定!
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 从 Windows Forms 到微服务的经验教训
· 李飞飞的50美金比肩DeepSeek把CEO忽悠瘸了,倒霉的却是程序员
· 超详细,DeepSeek 接入PyCharm实现AI编程!(支持本地部署DeepSeek及官方Dee