ASIHTTPRequest, an open-source HTTP packaging project for iPhone applications

Source: Internet
Author: User

ASIHTTPRequestA powerfulHTTPThis article describes the packaging of open-source projects,ASIHTTPRequestWhat is it?ASIHTTPRequestIt is easy to use and encapsulates CFNetwork APIs. Makes it easier to communicate with Web servers. It is written in Objective-C and can be used in mac OS X andIPhoneApplication.

It applies to basic executionHTTPRequests and interactions or feedback ). ASIFormDataRequest can be used to submit data and files. Use multipart/form-data

Provides the following:

An interface for submitting and retrieving data from the web Server

Download data directly to the memory or local file system

The ability to submit files locally as part of post data. Compatible with HTML file input mechanisms

Attackers can access and modify http request and response headers.

Obtain upload and download progress information

Asynchronous requests and queues to automatically manage the upload/download queue mechanism

Cookie support

Gzip support for requests and responses

Proxy request

ASIHTTPRequest settings

Use ASIHTTPRequest in iphone Projects

1. Add some necessary files and copy the following files to the project.

 
 
  1. ASIHTTPRquestConfig.h  
  2. ASInputStream.h  
  3. ASInputStream.m  
  4. ASIHTTPRequest.h  
  5. ASIHTTPRequest.h  
  6. ASINSStringAdditions.h  
  7. ASINSStringAdditions.m  
  8. ASIFormDataRequest.h  
  9. ASIFormDataRequest.m  
  10. ASINetworkQueue.h  
  11. ASINetworkQueue.m 

The iphone project must also contain the following files

 
 
  1. ASIAuthenticationDialog.h  
  2. ASIAuthenticationDialog.m 

One version of Reachability class

Add necessary frameworks to the Project

 
 
  1. CFNetwork.framework  
  2. SystemConfiguration.framework  
  3. libz.1.2.3.dylib 

Configure Reachability

On the iphone, ASIHTTPRequest uses Apple's Reachability class.

Reachability has two versions, which can be found in the Reachability folder of the ASIHTTPRequest release file.

Version 2.0 is the latest version. If your project is based on iphone OS 3.x and an updated system, you should use version 2.0. Including. h and. m files. Ensure that the value of REACHABILITY_20_API in the ASIHTTPRequestConfig. h file is 1

1.5 is an old version. It is compatible with iphone OS 2.2.1-iphone OS 3.0. Ensure that the value of REACHABILITY_20_API in the ASIHTTPRequestConfig. h file is 0.

Use AHIHTTPRequest in mac ox x

To use ASIHTTPRequest in Mac OS x projects, you need to import the following:

 
 
  1. SystemConfiguration.framework + zlib  
  2. CoreService.framework 

On Mac OS X, CFNetwork is part of the CoreServices framework. Unless you are writing a console-based application, the official address: http://allseeing-i.com/ASIHTTPRequest/

ASIHTTPRequest is an open-source project directly on the CFNetwork. It provides an encapsulation of HTTP network transmission that is more convenient and powerful than the official website.

Features:

1. The downloaded data is saved directly to the memory or file system.

2. provides APIs for directly submitting (http post) files

3. You can directly access and modify HTTP request and response headers.

4. Easy access to upload and download progress information

5. asynchronous requests and queues for automatic management of upload and download queue management machines

6. authentication and authorization support

7. Cookie

8. Request and response GZIP

9. proxy request

Here are two small examples:

 
 
  1. NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];  
  2. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];  
  3. [request start];  
  4. NSError *error = [request error];  
  5. if (!error) {  
  6. NSString *response = [request responseString];  

When you need to add more request information, such as adding a request Header:

 
 
  1. [request addRequestHeader:@"name" value:@"Jory lee"]; 

Key value when adding a Post request:

 
 
  1. [request setPostValue:@"Ben" forKey:@"first_name"];  
  2. [request setPostValue:@"Copsey" forKey:@"last_name"];  
  3. [request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"]; 

Set the HTTP Authorization account:

 
 
  1. [request setUsername:@"username"];  
  2. [request setPassword:@"password"]; 

An asynchronous request:

 
 
  1. - (IBAction)grabURLInBackground:(id)sender  
  2. {  
  3. NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];  
  4. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];  
  5. [request setDelegate:self];  
  6. [request startAsynchronous];  
  7. }  
  8. - (void)requestFinished:(ASIHTTPRequest *)request  
  9. {  
  10. // Use when fetching text data  
  11. NSString *responseString = [request responseString];  
  12. // Use when fetching binary data  
  13. NSData *responseData = [request responseData];  
  14. }  
  15. - (void)requestFailed:(ASIHTTPRequest *)request  
  16. {  
  17. NSError *error = [request error];  
  18. }    

In the process of data acquisition, if the data source is complex, a request queue is essential:

 
 
  1. - (IBAction)grabURLInTheBackground:(id)sender  
  2. {  
  3. if (![self queue]) {  
  4. [self setQueue:[[[NSOperationQueue alloc] init] autorelease]];  
  5. }  
  6. NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];  
  7. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];  
  8. [request setDelegate:self];  
  9. [request setDidFinishSelector:@selector(requestDone:)];  
  10. [request setDidFailSelector:@selector(requestWentWrong:)];  
  11. [[self queue] addOperation:request]; //queue is an NSOperationQueue  
  12. }  
  13. - (void)requestDone:(ASIHTTPRequest *)request  
  14. {  
  15. NSString *response = [request responseString];  
  16. }  
  17. - (void)requestWentWrong:(ASIHTTPRequest *)request  
  18. {  
  19. NSError *error = [request error];  
  20. }    

Summary:IPhoneApplication in progressHTTPPackage open-source projectsASIHTTPRequestI hope this article will help you!

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.