iOS development asihttprequest send data and download data

Source: Internet
Author: User
Tags empty

Send Data

This article introduces the contents of iOS development asihttprequest sending data, including setting request headers, using asiformdatarequest post forms, put requests, custom post requests, and so on.

 Set Request Header

ASIHTTPRequest *request = [ASIHTTPRequest Requestwithurl:url];

[Request addrequestheader:@ "Referer" value:@ "http://www.dreamingwish.com/"];

Using the Asiformdatarequest post form

Usually the data is sent in ' application/x-www-form-urlencoded ' format, and if binary data or files are uploaded, the format will automatically become ' Multipart/form-data '.

The data in the file is loaded from disk when needed, so uploading large files is no problem as long as the Web server can handle it.

Asiformdatarequest *request = [Asiformdatarequest Requestwithurl:url];

[Request setpostvalue:@ "Ben" forkey:@ "first_name"];

[Request setpostvalue:@ "Copsey" forkey:@ "last_name"];

[Request setfile:@ "/users/ben/desktop/ben.jpg" forkey:@ "photo"];

The MIME header of the data is automatically determined, but if you want to customize the MIME header, then this:

Asiformdatarequest *request = [Asiformdatarequest Requestwithurl:url];

Upload a file on disk

[Request setfile:@ "/users/ben/desktop/ben.jpg" withfilename:@ "myphoto.jpg" andcontenttype:@ "Image/jpeg"

forkey:@ "Photo"];

Upload an NSData instance

[Request Setdata:imagedata withfilename:@ "myphoto.jpg" andcontenttype:@ "image/jpeg" forkey:@ "photo"];

You can use the Addpostvalue method to send multiple data of the same name (Dream dimension: The server will render as an array):

Asiformdatarequest *request = [Asiformdatarequest Requestwithurl:url];

[Request addpostvalue:@ "Ben" forkey:@ "names"];

[Request addpostvalue:@ "George" forkey:@ "names"];

[Request addfile:@ "/users/ben/desktop/ben.jpg" forkey:@ "photos"];

[Request Adddata:imagedata withfilename:@ "george.jpg" andcontenttype:@ "image/jpeg" forkey:@ "photos"];

  Put request, custom POST request

If you want to send a put request, or you want to customize a POST request, use Appendpostdata: or appendpostdatafromfile:

ASIHTTPRequest *request = [ASIHTTPRequest Requestwithurl:url];

[Request appendpostdata:[@ ' is the My Data ' datausingencoding:nsutf8stringencoding]];

Default becomes POST when to use Appendpostdata:/appendpostdatafromfile:/setpostbody:

[Request setrequestmethod:@ "put"];

 Download data

This article introduces the contents of the iOS development asihttprequest download data, which includes setting the server response data to download directly to the file, processing the received server response data, putting request, obtaining the HTTP status code, reading the response Head, processing the text encoding, processing redirects and so on.

Download server response data directly to a file

If you request a large resource, you can save memory by downloading the data directly to the file. At this point, asihttprequest will not keep all the returned data in memory at once.

When we download the data to Downloaddestinationpath, the data will be first in the temporary file. The path name of the file is stored in Temporaryfiledownloadpath (Dream dimension: If you do not set this value, a filename is automatically generated, in the emulator, the file is created in $tmpdir).

ASIHTTPRequest *request = [ASIHTTPRequest Requestwithurl:url];

[Request setdownloaddestinationpath:@ "/users/ben/desktop/my_file.txt"];

When the request completes, one of the following two things happens:

If the data is compressed (gzip), the compressed file will be uncompressed to Downloaddestinationpath and the temporary file will be deleted.

If the data is not compressed, the file will be moved to Downloaddestinationpath, and the conflict resolution is: Overwrite the existing file.

Note: If the server response data is empty, then the file will not be created. If your return data may be empty, you should first check that the download file exists, and then manipulate the file.

  Processing received server response data

If you want to process the server response data (for example, you want to use the stream parser to process the data stream being downloaded), you should implement the proxy function Request:didreceivedata:. Note that if you do this, ASIHTTPRequest will not populate responsedata to memory and will not write data to the file (Downloaddestinationpath)-you have to handle both of these things yourself.

 Get HTTP status Code

ASIHTTPRequest does not do any processing of HTTP status codes (except for redirects and authorization status codes, as described below), so you must check the status values and handle them correctly.

ASIHTTPRequest *request = [ASIHTTPRequest Requestwithurl:url];

[Request startsynchronous];

int statusCode = [request Responsestatuscode];

NSString *statusmessage = [request Responsestatusmessage];

  Read response headers

ASIHTTPRequest *request = [ASIHTTPRequest Requestwithurl:url];

[Request startsynchronous];

NSString *poweredby = [[Request Responseheaders] objectforkey:@ "x-powered-by"];

NSString *contenttype = [[Request Responseheaders] objectforkey:@ "Content-type"];

Working with text encoding

ASIHTTPRequest will attempt to read the encoded information (Content-type header information) of the returned data. If it finds encoded information, it sets the encoding information to the appropriate nsstringencoding. If it does not find the encoded information, it sets the encoding to the default encoding (nsisolatin1stringencoding).

When you call [request Responsestring],asihttprequest will try to convert the returned data to NSString responseencoding.

  Handling redirection

When one of the following HTTP status codes is encountered, ASIHTTPRequest is automatically redirected to the new URL:

Permanently moved

302 Found

303 and other

When redirection occurs, the value of the response data (responseheaders,responsecookies,responsedata,responsestring, and so on) will be mapped to the corresponding return data for the final address.

When a URL occurs with circular redirection, the cookie that is set at this URL is stored in the global domain and sent to the server at the appropriate time with the redirected request.

Cookies set on any of the URLs encountered during a redirection cycle would be stored in the global cookie store, and would Be represented to the server on the redirected request when appropriate.

You can turn off automatic redirection: Set Shouldredirect to No.

By default, automatic redirection uses a GET request (empty the request body). This behavior is consistent with the behavior of most browsers, but HTTP spec 301 and 302 redirects must use the original method.

To redirect 301, 302 using the original method (including the body of the request), set Shoulduserfc2616redirectbehaviour to Yes before initiating the request.

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.