Mentioned earlier afnetworking is a very powerful network tripartite library, first you need to introduce the afnetworking three-party library, such as the wrong package also please point out, thank you!
1.Get Request
/**get Request
URL Server request Address
Results returned by the Success server response
Faile information about the failure
*/
+ (void) Gethttprequesturl: (nsstring *) URL requestsuccess: (void (^) (id repoes,nsurlsessiondatatask *task)) success Requestfaile: (void (^) (nserror *error)) faile{
Afhttpsessionmanager *manager = [Afhttpsessionmanager manager];
Manager.responseserializer = [Afhttpresponseserializer serializer];//abort parsing
Custom HTTP header can be omitted here
[Manager.requestserializer setvalue:@ "Application/json" forhttpheaderfield:@ "Accept"];
[Manager.requestserializer setvalue:@ "Application/json;charset=utf-8" forhttpheaderfield:@ "Content-Type"];
[Manager.requestserializer setvalue:@ "HTTP Header" forhttpheaderfield:@ "Xiaoguige"];
[Manager.requestserializer Sethttpshouldhandlecookies:no];
[Manager Get:url Parameters:nil progress:^ (nsprogress * _nonnull downloadprogress) {
} success:^ (Nsurlsessiondatatask * _nonnull task, id _nullable responseobject) {
if (success) {
Return successful results
Success (Responseobject,task);
}
} failure:^ (Nsurlsessiondatatask * _nullable task, Nserror * _nonnull error) {
Return failed results
Faile (Error);
}];
}
2.Post Request
/**post Request
URL Server request Address
Pram Request Parameters
Results returned by the Success server response
Faile information about the failure
*/
+ (void) Posthttprequesturl: (nsstring *) URL Requestpram: (ID) pram requestsuccess: (void (^) (id respoes)) success Requestfaile: (void (^) (nserror *erro)) faile{
Afhttpsessionmanager *manager = [Afhttpsessionmanager manager];
[Manager Post:url Parameters:pram progress:^ (nsprogress * _nonnull uploadprogress) {
This is progress.
} success:^ (Nsurlsessiondatatask * _nonnull task, id _nullable responseobject) {
if (success) {
Return successful results
Success (Responseobject);
}
} failure:^ (Nsurlsessiondatatask * _nullable task, Nserror * _nonnull error) {
Return failed results
Faile (Error);
}];
}
3. File download
+ (void) createdownloadfilewithurlstring: (NSString *) urlstring downloadfileprogress: (void (^) (nsprogress *)) Downloadfileprogress Setupfilepath: (Nsurl * (^) (Nsurlresponse *)) Setupfilepath Downloadcompletionhandler: (void (^) ( Nsurl *, Nserror *)) downloadcompletionhandler{
Nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl urlwithstring:urlstring];
Afhttpsessionmanager *manager = [Afhttpsessionmanager manager];
Manager.requestserializer = [Afhttprequestserializer serializer];
Manager.responseserializer = [Afhttpresponseserializer serializer];
Nsurlsessiondownloadtask *datatask = [Manager downloadtaskwithrequest:request progress:^ (nsprogress * _Nonnull DownloadProgress) {
/**
* Download Progress
*/
Downloadfileprogress (downloadprogress);
} Destination:^nsurl * _nonnull (Nsurl * _nonnull TargetPath, Nsurlresponse * _nonnull response) {
/**
* Set Save Directory
*/
return Setupfilepath (response);
} completionhandler:^ (Nsurlresponse * _nonnull response, Nsurl * _nullable filePath, Nserror * _nullable error) {
/**
* Download complete
*/
Downloadcompletionhandler (Filepath,error);
}];
[Datatask resume];
}
If there are errors, also hope to point out!
iOS Development-afnetworking Package get (custom HTTP Header) and POST request and file download