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.
- ASIHTTPRquestConfig.h
- ASInputStream.h
- ASInputStream.m
- ASIHTTPRequest.h
- ASIHTTPRequest.h
- ASINSStringAdditions.h
- ASINSStringAdditions.m
- ASIFormDataRequest.h
- ASIFormDataRequest.m
- ASINetworkQueue.h
- ASINetworkQueue.m
The iphone project must also contain the following files
- ASIAuthenticationDialog.h
- ASIAuthenticationDialog.m
One version of Reachability class
Add necessary frameworks to the Project
- CFNetwork.framework
- SystemConfiguration.framework
- 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:
- SystemConfiguration.framework + zlib
- 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:
- NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
- [request start];
- NSError *error = [request error];
- if (!error) {
- NSString *response = [request responseString];
- }
When you need to add more request information, such as adding a request Header:
- [request addRequestHeader:@"name" value:@"Jory lee"];
Key value when adding a Post request:
- [request setPostValue:@"Ben" forKey:@"first_name"];
- [request setPostValue:@"Copsey" forKey:@"last_name"];
- [request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];
Set the HTTP Authorization account:
- [request setUsername:@"username"];
- [request setPassword:@"password"];
An asynchronous request:
- - (IBAction)grabURLInBackground:(id)sender
- {
- NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
- [request setDelegate:self];
- [request startAsynchronous];
- }
- - (void)requestFinished:(ASIHTTPRequest *)request
- {
- // Use when fetching text data
- NSString *responseString = [request responseString];
- // Use when fetching binary data
- NSData *responseData = [request responseData];
- }
- - (void)requestFailed:(ASIHTTPRequest *)request
- {
- NSError *error = [request error];
- }
In the process of data acquisition, if the data source is complex, a request queue is essential:
- - (IBAction)grabURLInTheBackground:(id)sender
- {
- if (![self queue]) {
- [self setQueue:[[[NSOperationQueue alloc] init] autorelease]];
- }
- NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
- [request setDelegate:self];
- [request setDidFinishSelector:@selector(requestDone:)];
- [request setDidFailSelector:@selector(requestWentWrong:)];
- [[self queue] addOperation:request]; //queue is an NSOperationQueue
- }
- - (void)requestDone:(ASIHTTPRequest *)request
- {
- NSString *response = [request responseString];
- }
- - (void)requestWentWrong:(ASIHTTPRequest *)request
- {
- NSError *error = [request error];
- }
Summary:IPhoneApplication in progressHTTPPackage open-source projectsASIHTTPRequestI hope this article will help you!