Introduction to ASI Usage (post and file upload)

Source: Internet
Author: User

The basic use of the ASI is described in the previous section, and the default usage is in the GET request mode. If you have any questions, please click here. A GET request is a parameter that is passed from a URL, and is generally intended to "ask" the server for the data it wants. And the post request, is generally the form data submission, the request data information in the request message, the background server receives the request information, will carry on the corresponding processing, for example: the login operation. In this section, I will use the POST request in the ASI to do some simple introduction.


In the ASI, the class used for the POST request is asiformdatarequest.

Example 1: web version 163 email login.

-(void) postrequest {    Nsurl *url = [Nsurl urlwithstring:@ "http://mail.163.com/"];    Self.request = [Asiformdatarequest requestwithurl:url];        [Self.request setpostvalue:@ "username" forkey:@ "your username"];    [Self.request setpostvalue:@ "password" forkey:@ "your password"];        Self.request.timeOutSeconds = ten;        __weak typeof (Self.request) that = Self.request;    self.request.completionblock= ^{        NSLog (@ "data:%@", that.responsestring);    };        [Self.request startasynchronous];}

If the login is successful, responsestring returns the data, which is the HTML source code of the successful Mailbox list page of your landing page.


Example 2: Upload photos of your phone albums to the server.

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event {[Self uploadpicturefromalbum];} #pragma mark-Open the photo selector controller (that is, the album in your phone)-(void) Uploadpicturefromalbum {//1. Album Controller Uiimagepickercontroller *pickercontrol        Ler = [[Uiimagepickercontroller alloc] init];        Set up the agent, the picture after the completion of the choice of things to do pickercontroller.delegate = self; 2.        Set type (photo) Pickercontroller.sourcetype = uiimagepickercontrollersourcetypephotolibrary; 2.        Set the type (take photo) and then get the picture after the photo//pickercontroller.sourcetype = Uiimagepickercontrollersourcetypecamera; 3. Pop-up photo [self presentviewcontroller:pickercontroller animated:yes completion:nil];} #pragma mark-When the photo selection is complete, the callback returns-(void) Imagepickercontroller: (Uiimagepickercontroller *) picker Didfinishpickingmediawithinfo: (nsdictionary *) Info {//1. When you finish selecting the picture, close Uiimagepickercontroller [Picker Dismissviewcont        Rolleranimated:yes Completion:nil];        Get picture Stream object UIImage *image = Info[uiimagepickercontrolleroriginalimage]; [Self UploadwithimaGe:image];} #pragma mark-Upload a local album image, binary object (Stream object)-(void) Uploadwithimage: (UIImage *) Image {Nsurl *posturl = [Nsurl urlwithstring:@ "PO        St site URL "];        1. Establishment request Asiformdatarequest *request = [Asiformdatarequest Requestwithurl:posturl];    2. The form of the stream specifies the uploaded file NSData *data = uiimagepngrepresentation (image);        [Request Setdata:data withfilename:@ "Uploadfilename" andcontenttype:@ "image/png" forkey:@ "my Picture"];    3.POST other parameters [request setpostvalue:@ "Jason" forkey:@ "username"];        [Request setpostvalue:@ "123" forkey:@ "password"];        4. Send the request [requests startasynchronous];    5. Listening Request Request.completionblock = ^{NSLog (@ "upload complete");    }; }

This section briefly describes the application of the POST request in the ASI, and in the next section, I will cover the download of network resources using ASI.


Introduction to ASI Usage (post and file upload)

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.