The main content of this paper is to talk about the afurlrequestserialization in afnetworking. Its main function is to help us create nsmutableurlrequest and encapsulate the required parameters into nsmutableurlrequest when we are sending a network request. What it does inside it, what features it provides, makes it so easy and easy for us to make network requests. It seems that we can build a correct request body nsurlrequest without any control. The next step is to uncover its mysterious veil.
Overview
First look at the classes contained in AFURLRequestSerialization.h and afurlrequestserialization.m and the relationships between them, and the functions of each part.
Afurlrequestserialization protocol: Defines a method such that each subclass will have its own implementation according to its own needs
-(Nullable Nsurlrequest *) Requestbyserializingrequest: (Nsurlrequest *) Request Withparameters: (Nullable ID) Parameters * _nullable __autoreleasing *) error
Here is a pseudo-code description afhttprequestserializer, Afjsonrequestserializer, Afpropertylistrequestserializer function ( This is very important!!!) ).
if(The request is in the following way:Get||Head|| Delete) {Afhttprequestserializer, Afhttprequestserializer, Afhttprequestserializer
Actually, it's all transferred to Afhttprequestserializer .- (Nsurlrequest *) Requestbyserializingrequest: (Nsurlrequest *Request Withparameters: (ID) Parameters Error: (nserror *__autoreleasing *) Error
The parameters is then spelled into URL parameters appended to the URL as part of the request. } Else if(form submission) {Afhttprequestserializer, Afhttprequestserializer, Afhttprequestserializer will
are called to Afhttprequestserializer.- (Nsmutableurlrequest *) Multipartformrequestwithmethod: (NSString *) method URLString: (nsstring *) urlstring parameters: (nullable nsdictionary <nsstring *, id> *) Parameters Constructingbodywithblock: (Nullablevoid(^) (ID <AFMultipartFormData>formData)) Block error: (nserror * _nullable __autoreleasing *) error; In this case, the afstreamingmultipartformdata will be used, at this time parameters and through the Afstreamingmultipartformdata Tim
The added data will change the afhttpbodypart of an item to be saved, and when the uploading system needs to read the data, it will fetch the data through Afhttpbodypart. } Else //i.e. ((put| | post| | Patch) &&! When single commit){afhttprequestserializer: After stitching the parameters into a string (how to stitch the details behind)
Serialize into NSData into the httpbody of HttpRequest.
Afjsonrequestserializer: Serialization of parameters in Datawithjsonobject mode
Into the nsdata into the HttpRequest httpbody.
Afpropertylistrequestserializer: Parameters the request to Datawithpropertylist
The mode is serialized into NSData into the httpbody of the HttpRequest. }
Afmultipartformdata protocol: Defines a number of interface methods that allow users to add the contents of a form in different ways, such as using file paths, working directly with NSData, or using inputstream.
Afstreamingmultipartformdata: Following the Afmultipartformdata protocol, the methods of the Protocol have been implemented.
Afmultipartbodystream: It plays an important role in bridging the form data when the system is first transferred to it, and then it relies on Afhttpbodypart to read the data and then return the data to the system call.
Afhttpbodypart: Each afhttpbodypart represents a form of data that really reads its internal data (regardless of the form: file path, NSData, or Nsinputstream).
Implementation Details
Next (to be continued ...) )
iOS afnetworking 3.0 Original Code reading Analysis (i) (afurlrequestserialization)