Description and configuration of swifthttp
1, what is swifthttp
The Swifthttp essence is based on ' nsurlsession ' and has been encapsulated. Using Swifthttp can make our network request related code (such as getting data, submitting data, uploading files, downloading files, etc.) more concise and easy to use. (a bit similar to another network library I introduced in my previous article: Alamofire)
Functional characteristics of 2,swifthttp:
(1) Convenient closure API
(2) Support run queue (nsoperationqueue)
(3) Support parameter coding
(4) built-in JSON request serialization
(5) Upload/download files with progress
(6) Code Concise
Installation and configuration of 3,swifthttp
(1) Download the latest code from GITHUB: Https://github.com/daltoniam/SwiftHTTP
(2) will download the source code package swifthttp.xcodeproj dragged to your project
(3) Project-> general-> Embedded binaries item, increase Swifthttp.framework
(4) Finally, in the need to use Swifthttp place import to come in
Import Swifthttp
Second, use Swifthttp to make data requests
1,get Request
(1) with no parameters
do {
Let opt = Try HTTP. Get ("Http://111cn.net")
Opt.start {response in
If let Err = response.error {
Print ("Error: \ (err.localizeddescription)")
Return
}
Print ("Get to Data: \ (response.description)")
}
Catch let error {
Print ("Request failed: \ (Error)")
}
(2) with parameters
do {
The actual URL is: http://111cn.net?hello=world¶m2=1
Let opt = Try HTTP. Get ("Http://111cn.net", Parameters: ["Hello": "World", "param2": 1])
Opt.start {response in
If let Err = response.error {
Print ("Error: \ (err.localizeddescription)")
Return
}
Print ("Get to Data: \ (response.description)")
}
Catch let error {
Print ("Request failed: \ (Error)")
}
2,post Request
let params = ["param": "param1",
"Array": ["Second", "third"],
"num": 23,
"Dict": ["Somekey": "Someval"]]
do {
Let opt = Try HTTP. POST ("Http://www.111cn.net", Parameters:params)
Opt.start {response in
Perform specific response actions ...
}
Catch let error {
Print ("Request failed: \ (Error)")
}
3, support for custom HTTP header information (HTTP Headers)
do {
Let opt = Try HTTP. Get ("Http://www.111cn.net", Parameters: ["Hello": "There"],
Headers: ["header": "Value"])
Opt.start {response in
Perform specific response actions ...
}
Catch let error {
Print ("Request failed: \ (Error)")
}
Third, use the action queue for the request
Swifthttp also supports the run queue (Operation queue), where we can put all data requests back into the queue and automate multi-threaded asynchronous requests.
At the same time, through the Maxconcurrentoperationcount property, you can set a maximum number of simultaneous requests at the same times.
Let Operationqueue = Nsoperationqueue ()
Operationqueue.maxconcurrentoperationcount = 2//maximum number of tasks
do {
Let opt1 = Try HTTP. New ("Http://www.111cn.net", Method:. Get)
Opt1.onfinish = {response in
Perform specific response actions ...
}
Operationqueue.addoperation (OPT1)
Let Opt2 = Try HTTP. New ("Http://www.baidu.com", Method:. Get)
Opt2.onfinish = {response in
Perform specific response actions ...
}
Operationqueue.addoperation (OPT2)
Let OPT3 = Try HTTP. New ("Http://www.111cn.net", Method:. Get)
Opt3.onfinish = {response in
Perform specific response actions ...
}
Operationqueue.addoperation (OPT3)
Catch let error {
Print ("Request failed: \ (Error)")
}
You can use the Cancel () method to stop a single task:
Opt2.cancel ()//Cancel this request task
You can also cancel all tasks by Nsoperationqueue the Cancelalloperations () method:
Operationqueue.cancelalloperations ()//Cancel all thread operations