iOS Development-afnetworking Package get (custom HTTP Header) and POST request and file download

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.