How IOS develops to upload data via put request _ios

Source: Internet
Author: User


1, what is put request



The put request is a request that the server store a resource and use Request-uri as its identity, similar to post, but not commonly used, and put requests are relatively rare. This is not supported by HTML forms either. In essence, put and post are very similar to send data to the server, but there is an important difference between them, put usually specifies the location of the resources, and post is not, post data storage location by the server itself. For example, a url,/addblog for submitting a blog post. If you use put, the URL you submit will be "/addblog/abc123" like this, where abc123 is the address of this blog post. If you use post, the address will be communicated to the client by the server after it is submitted. This is the way most blogs are now. Obviously, the put and post uses are not the same. Which also depends on the current business scenario.



2, post and put request the fundamental difference



There is a view that post should be used to create a resource to update a resource with put; there is a view that you should use put to create a resource, post to update a resource, and that you can create or update a resource with any of the put and post. These views only see the style, the debate is just arguing about which style is better, in fact, put or post, not to see whether this is to create or update the resources of the action, this is not a question of style, but a semantic problem. In HTTP, put is defined as a idempotent method, post is not, this is a very important difference.



3, how to apply in the code



Let's take a look at how the put and post methods are used in afnetworking:


Post Method
-(Nullable Nsurlsessiondatatask *) Post: (NSString *) urlstring
        parameters: (nullable ID) parameters
    Constructingbodywithblock: (Nullable Void (^) (id <AFMultipartFormData> formData)) Block
        progress: ( nullable void (^) (nsprogress *uploadprogress)) uploadprogress
        success: (Nullable void (^) (Nsurlsessiondatatask * Task, id _nullable responseobject)) Success
        failure: (Nullable void (^) (Nsurlsessiondatatask * _nullable task, Nserror *error)) failure;

Put method
-(Nullable Nsurlsessiondatatask *) put: (NSString *) urlstring
     parameters: (nullable ID) parameters
      success: (Nullable void (^) (Nsurlsessiondatatask *task, id _nullable responseobject)) Success
      failure: ( nullable void (^) (Nsurlsessiondatatask * _nullable task, Nserror *error)) failure;


The discovery parameter only urlstring and parameters, is less than the POST request the Formdata parameter, does not have the place to splice the data to go up, I once tried to put the information in the parameters does not use, Search on the Internet a lot also did not find a suitable solution, can only change one way.



4. Solutions



After using the afnetworking to make a put request bad, I thought about whether to use the system Nsurlsessiontask to implement put request, but think of yourself write too troublesome, try to use ASIHTTPRequest to try, Found ASIHTTPRequest is still very useful.



The following code is posted:


-(void) Uploaddata: (NSData *) data uploadurl: (NSString *) uploadurl {asihttprequest 
 * fileuprequest = [ ASIHTTPRequest Requestwithurl:[nsurl Urlwithstring:uploadurl]];
 Fileuprequest.delegate = self;
 Set the request timeout time to 60 seconds
 [Fileuprequest settimeoutseconds:60.f];
 Set the number of times the request was tried again after the request timeout
 [fileuprequest setnumberoftimestoretryontimeout:2];
 Splicing data into
 [Fileuprequest appendpostdata:data];
 Set the request mode to put
 [fileuprequest setrequestmethod:@ ' put '];
 Sets the callback method for the upload failure
 [fileuprequest setdidfailselector: @selector (uploadfailed:)];
 Set the callback method to upload successfully
 [fileuprequest setdidfinishselector: @selector (uploadfinished:)];
 Show accurate upload progress
 fileuprequest.showaccurateprogress = YES;
 Start asynchronous Request
 [Fileuprequest startasynchronous];
}


Summarize



Here my problem has been resolved, the above is the entire content of this article, I hope this article on the content of iOS developers can help, if you have questions you can message exchange.


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.