nsurlsession

Right Way for changing timeoutIntervalForRequest in Alamofire

对着背影说爱祢 提交于 2020-02-15 08:38:14
问题 I changed timeoutIntervalForRequest with let manager = Alamofire.SessionManager.default manager.session.configuration.timeoutIntervalForRequest = 3 manager.request(url).response {} but seems not worked, Any suggestion? 回答1: This worked for me :) let configuration = URLSessionConfiguration.default configuration.timeoutIntervalForRequest = TimeInterval(7) configuration.timeoutIntervalForResource = TimeInterval(7) let session = URLSession(configuration: configuration) let task = session.dataTask

Right Way for changing timeoutIntervalForRequest in Alamofire

时间秒杀一切 提交于 2020-02-15 08:37:46
问题 I changed timeoutIntervalForRequest with let manager = Alamofire.SessionManager.default manager.session.configuration.timeoutIntervalForRequest = 3 manager.request(url).response {} but seems not worked, Any suggestion? 回答1: This worked for me :) let configuration = URLSessionConfiguration.default configuration.timeoutIntervalForRequest = TimeInterval(7) configuration.timeoutIntervalForResource = TimeInterval(7) let session = URLSession(configuration: configuration) let task = session.dataTask

How to make an HTTP request in Swift?

折月煮酒 提交于 2020-02-08 12:15:12
问题 I read The Programming Language Swift by Apple in iBooks, but cannot figure out how to make an http request (something like CURL) in Swift. Do I need to import Obj-C classes or do I just need to import default libraries? Or is it not possible to make an HTTP request based on native swift code? 回答1: You can use URL , URLRequest and URLSession or NSURLConnection as you'd normally do in Objective-C. Note that for iOS 7.0 and later, URLSession is preferred. Using URLSession Initialize a URL

How to make an HTTP request in Swift?

瘦欲@ 提交于 2020-02-08 12:14:53
问题 I read The Programming Language Swift by Apple in iBooks, but cannot figure out how to make an http request (something like CURL) in Swift. Do I need to import Obj-C classes or do I just need to import default libraries? Or is it not possible to make an HTTP request based on native swift code? 回答1: You can use URL , URLRequest and URLSession or NSURLConnection as you'd normally do in Objective-C. Note that for iOS 7.0 and later, URLSession is preferred. Using URLSession Initialize a URL

Ocassional crash on the app start

天涯浪子 提交于 2020-02-05 03:26:46
问题 I'm getting very strange crash at the start of the app. My app spawns a lot of threads (over 500, most of them are related to PLPreheatItem, I can not paste full crash log here, cause stackoverflow has limit to 30K symbols). The problem is that I can not reproduce it on my device. Also it happens on clean install for my customer almost every run. I am using NSURLSession with background session and NSOperationQueue with 4 concurrent operations. These are the most weak parts as I think. Could

HTTP原理 请求方法

两盒软妹~` 提交于 2020-01-30 19:20:13
HTTP的工作过程 一次HTTP操作称为一个事务,其工作过程分为四步: 1.客户机与服务器建立连接:客户单击某个超级链接,HTTP的工作开始,接下来进行TCP连接的三次握手过程。 2.建立连接后,客户几发送一个请求给服务器,请求方式的格式为:统一资源标识符(URL)、协议版本号、MIME信息(包括请求修饰符、客户机信息和可能的内容)。 3.服务器接到请求后,给予相应的响应信息,其格式为:一个状态行(包括信息的协议版本号)、一个成功或错误的代码、后面的是MIME信息(包括服务器信息、实体信息、可能的内容)。 4.客户端接收到服务器所返回的信息,通过浏览器显示在用户的显示屏上,然后客户机与服务器断开连接。客户端收到服务器信息后,向服务器发送一个确认包,此包发送完毕,表示完成三次握手。 ——————————————————————————————————————— HTTP协议的主要特点: 1.支持客户/服务器模式-》过程 2.简单快速:客户向服务器请求服务时,只需发送请求方法(常用的有GET/POST/HEAD)和路径。每种请求方法规定了客户与服务器联系的类型不同,由于HTTP协议简单,使得HTTP服务器的程序规模小,因而通信速度快。 3.灵活:HTTP允许传输任意类型的数据对象,正在传输的类型用Content-Type加以标记。 4.无连接:限制每次连接只处理一个请求

【原】AFNetworking源码阅读(三)

爱⌒轻易说出口 提交于 2020-01-30 09:55:22
【原】AFNetworking源码阅读(三) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇 的话,主要是讲了如何通过构建一个request来生成一个data task。但是对于NSURLSession部分却没有提及。主要是精力有限,准备在这一部分把NSURLSession的知识好好梳理一遍。一切先从上一篇中的addDelegateForDataTask:函数说起,然后再介绍AFURLSessionManagerTaskDelegate,最后结合AFURLSessionManager中的NSURLSession梳理一遍(可能会将部分内容放到下一篇)。 2. 由addDelegateForDataTask引发 注意addDelegateForDataTask:这个函数并不是AFURLSessionManagerTaskDelegate的函数,而是AFURLSessionManager的一个函数。这也侧面说明了 AFURLSessionManagerTaskDelegate和NSURLSessionTask的关系是由 AFURLSessionManager管理的 。 该函数除了对于AFURLSessionManagerTaskDelegate类型的成员变量delegate设置之外,最关键的代码就是 [self setDelegate:delegate

源码分析--AFNetworking

不羁的心 提交于 2020-01-30 09:53:36
/*--> */ /*--> */ [AFNetworking] 这个框架是如今 iOS 开发中不可缺少的组件之一。使用也是比较简单的,使用它来发出 HTTP 请求有两个步骤: /*--> */ /*--> */ 1. 以服务器的* 主机地址或者域名 *生成一个 AFHTTPSessionManager 的实例 2. 调用 `- GET:parameters:progress:success:failure:` 方法 AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[[NSURL alloc] initWithString:@"hostname"]]; [manager GET:@"relative_url" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"%@" ,responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"%@", error);

NSURLCache, together with NSURLSession, does not respect: Cache-Control: max-age:86000, private, must-revalidate

蓝咒 提交于 2020-01-24 10:17:05
问题 In AppDelegate.m, I configured: NSURLCache *sharedURLCache = [[NSURLCache alloc] initWithMemoryCapacity:20 * 1024 * 1024 diskCapacity:100 * 1024 * 1024 diskPath:@"FhtHttpCacheDir"]; Then the http request: - (void) testRestfulAPI{ NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [NSURLSession sessionWithConfiguration:config]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http:/

POST URLRequest doesn't work in Swift 4

旧时模样 提交于 2020-01-24 01:36:06
问题 I want to use HTTP request to post an image on Imgur. I used Postman first to make sure my API logic is correct. Here is my setting for Postman ( myid is client id): This request succeeded, but the request failed in Swift with URLRequest and URLSession (Status Code: 400). var request = URLRequest(url: self.uploadURL!) request.httpMethod = "POST" request.setValue("Client-ID " + myid, forHTTPHeaderField: "Authorization") let data = "image=\(base64String)&type=base64".data(using: .utf8,