Ios-network (introduction to the use of Asihttprequest)
Source: Internet
Author: User
<span id="Label3"></p><p><p></p></p><p><p>Using the HTTP Network request API in the iOS SDK is quite complicated and cumbersome to call, ASIHTTPRequest is a set of APIs that encapsulates the Cfnetwork API and is very simple to use, written in objective-c, can be very well applied in Mac OS x systems and iOS platform Applications. ASIHTTPRequest applies to basic HTTP requests, and to the interaction between rest-based Services.</p></p><p><p>ASIHTTPRequest features are very powerful, the main features are as Follows:</p></p> <ul> <ul> <li>• The task of submitting data to the server and obtaining data from the server is done through a simple interface</li> <li>L downloaded data that can be stored in memory or stored directly on disk</li> <li>L can upload local file to server</li> <li>L can easily access and manipulate the request and return HTTP header information</li> <li>L can get the progress information to upload or download, provide a better experience for the application</li> <li>L support upload or download queue, and can get progress information of queue</li> <li>Support for basic, digest, and NTLM authentication, authorization credentials are automatically maintained in the same session, and can be stored in Keychain (password management system for Mac and iOS operating Systems)</li> <li>L Support Cookies</li> <li>When the app (iOS 4+) is running in the background, the request can continue to run</li> <li>L Support gzip Compressed data</li> <li>The built-in Asidownloadcache class allows you to cache the data returned by the request so that even if no network can return the cached data results</li> <li>L asiwebpagerequest– can download the full page, including the page, style sheet, script and other resource files, and display in UIWEBVIEW/WEBVIEW. Pages of any size can be cached indefinitely, so that you can browse offline even if you don't have a network</li> <li>L Support client Certificate</li> <li>• Support for initiating HTTP requests through proxies</li> <li>L Support Bandwidth Throttling. On the iOS platform, you can automatically decide whether to limit bandwidth based on current network conditions, such as when using a Wwan (gprs/edge/3g) network, and without any restrictions when using WiFi</li> <li>L Support for continuous transmission of breakpoints</li> <li>L Support synchronous and asynchronous requests</li> </ul> </ul>2.1.1 Installation Instructions<p><p>If you want to use ASIHTTPRequest in an iOS project, you need to make a simple configuration in your project, with the following steps:</p></p>1) Add File<p><p>There are two ways to add a Third-party class library file to a Xcode project:</p></p><p><p>1. The first way, open in the Finder you need to add to a file or folder, open the item in Xcode that you want to add a file to, and then select the file or folder you want to add, drag it from the Finder to xcode, and then Release. In the popup dialog box, if the file has been copied to the project file directory, you do not need to check the "copy items" checkbox, or if the file is not copied to the project file directory, you will need to select the "copy items" check box so that Xcode will automatically copy the files to the project file Directory. As shown in the Following:<br><br></p></p><p><p>2. The second way, in xcode, right-click the group under which you want to add the file, select the "add files to" My Project ... menu, and in the pop-up file browse dialog box, Choose the file or folder you want to add. If you want to add a file that has been copied to the project file directory, you do not need to select the check box for copy items, or if the file is not copied to the project file directory, you need to select the "copy items" check box so that Xcode automatically copies the files to the project file Directory. As shown in the Following:<br><br></p></p><p><p>Based on the instructions above, add asihttprequest related files to the Xcode project with the following list of required files:</p></p><p><p>ASIHTTPRequestConfig.h</p></p><p><p>ASIHTTPRequestDelegate.h</p></p><p><p>ASIProgressDelegate.h</p></p><p><p>ASICacheDelegate.h</p></p><p><p>ASIHTTPRequest.h</p></p><p><p>Asihttprequest.m</p></p><p><p>ASIDataCompressor.h</p></p><p><p>Asidatacompressor.m</p></p><p><p>ASIDataDecompressor.h</p></p><p><p>Asidatadecompressor.m</p></p><p><p>ASIFormDataRequest.h</p></p><p><p>ASIInputStream.h</p></p><p><p>Asiinputstream.m</p></p><p><p>Asiformdatarequest.m</p></p><p><p>ASINetworkQueue.h</p></p><p><p>Asinetworkqueue.m</p></p><p><p>ASIDownloadCache.h</p></p><p><p>Asidownloadcache.m</p></p><p><p>ASIAuthenticationDialog.h</p></p><p><p>Asiauthenticationdialog.m</p></p><p><p>Reachability.h (under The External/reachability directory of the source Code)</p></p><p><p>REACHABILITY.M (under The External/reachability directory of the source Code)</p></p>2) Link Related class library<p><p>1. Select the item</p></p><p><p>2. Select the target</p></p><p><p>3. Jump to the "Build phases" tab</p></p><p><p>4. Expand the "Link Binary with Libraries" grouping</p></p><p><p>5. Click "+" to add the class library</p></p><p><p>As shown in the Following:</p></p><p><p></p></p><p><p>6. Select Cfnetwork.framework from the list and click the "Add" button.</p></p><p><p></p></p><p><p>7. Follow the same method as in the previous step to Add: systemconfiguration.framework, mobilecoreservices.framework, Coregraphics.framework and Libz.1.2.3.dylib are some of the class Libraries.</p></p><p><p>8. Once added, you can drag the added class library into the frameworks directory of the Xcode project<br></p></p>2.1.2 Instructions for use<p><p>ASIHTTPRequest has a number of features, all feature descriptions can be found in the relevant documents on its official website, limited to space, This chapter simply describes how to use ASIHTTPRequest to synchronize HTTP requests and asynchronous HTTP Requests. In later chapters, we'll also use some of its other features.</p></p><p><p>Before using asihttprequest, Make sure that you have installed it correctly, and then add it to the header of the code file that you want to apply it to:</p></p><p><p>#import "ASIHTTPRequest.h"</p></p><p><p>This allows you to use ASIHTTPRequest related classes in your Code.</p></p>Create a synchronization request<p><p>This is the simplest usage pattern for asihttprequest, which starts executing an HTTP request in the same thread after sending the <strong>startsynchronous</strong> message, and the thread waits until the request finishes (the request succeeds or fails). By checking the <strong>error</strong> property, You can determine whether the request was successful or an error occurred.</p></p><p><p>To get the returned text information, call the <strong>responsestring</strong> method. If you are downloading a binary file, a MP3, a <strong>responsedata</strong> method, You can get a nsdata object.</p></p><p><p>-(ibaction) graburl: (id) Sender</p></p><p><p>{</p></p><p><p>Nsurl *url = [nsurl urlwithstring:@ "http://allseeing-i.com"];</p></p><p><p>ASIHTTPRequest *request = [asihttprequest requestwithurl:url];</p></p><p><p>[request startsynchronous];</p></p><p><p>Nserror *error = [request error];</p></p><p><p>If (!error) {</p></p><p><p>NSString *response = [request responsestring];</p></p><p><p>}</p></p><p><p>}</p></p><p><p>In general, the asynchronous request should be used instead of the synchronous request, and when the ASIHTTPRequest synchronization request is used in the main thread, the Application's interface is locked and no action can be made until the request is Complete.</p></p>Create an asynchronous request<p><p>The synchronization request in the previous example, if called asynchronously, requests are run in a background thread, and then the calling thread is notified when the request has finished Executing. This does not cause the interface to be locked when the main thread makes a network Request.</p></p><p><p>-(ibaction) graburlinbackground: (id) Sender</p></p><p><p>{</p></p><p><p>Nsurl *url = [nsurl urlwithstring:@ "http://allseeing-i.com"];</p></p><p><p>ASIHTTPRequest *request = [asihttprequest requestwithurl:url];</p></p><p><p>[request setdelegate:self];</p></p><p><p>[request startasynchronous];</p></p><p><p>}</p></p><p><p>-(void) requestfinished: (asihttprequest *) Request</p></p><p><p>{</p></p><p><p>This method is used when the returned content is read as text</p></p><p><p>NSString *responsestring = [request responsestring];</p></p><p><p>This method is used when the returned content is read in binary form</p></p><p><p>NSData *responsedata = [request responsedata];</p></p><p><p>}</p></p><p><p>-(void) requestfailed: (asihttprequest *) Request</p></p><p><p>{</p></p><p><p>Nserror *error = [request error];</p></p><p><p>}</p></p><p><p>Ios-network (introduction to the use of Asihttprequest)</p></p></span>
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.