Asihttprequest series (1): synchronous and asynchronous requests.

Source: Internet
Author: User

Click Open Link

Asihttprequest Project address: Submit.

It provides the following functions:

Submit data to or obtain data from the Web server;

Download data to memory or disk;

Uses the same HTML input mechanism to upload files;

Resumable upload;

Simple access to HTTP headers;

Upload/download progress display;

Supports cookies;

Running in the background (supported by ios4.0 or above );

Gzip support for requests and responses;

Supports client certificates;

Support synchronous/asynchronous requests

Zookeeper

There are already a lot of online introductions about it. This project has a detailed guide document:
How to Use asihttprequest, some netizens have also translated it into Chinese. This article does not completely copy the content of the official document, but focuses on several common applications, and covers some of your own understanding and practical application experience, including:

Installation, simple asynchronous/Synchronous requests, queue requests, upload, download and cookies

Note: although these technologies are described separately in this article, they are often used in combination with various technologies in actual work.

In addition, HTTP requests are often accompanied by the application of XML technology. In fact, asihttprequest can be used in combination with Sax asynchronous parsing. For this part of content, see the author's another blog post "asihttprequest and libxml combination, implement edge request and edge resolution.

Project: http://github.com/pokeb/asi-http-request/tarball/master

Decompress the downloaded file to any directory.

Open this Directory, which contains:

An iphone xcode Project (source file)

A mac xcode Project (source file)

Sample Code (source file) used in an iPhone)

Sample Code (source file) used in a Mac)

A readme. texttile, about this project

In fact, all the content is here. If you are a beginner and don't know how to get started, you can see
Http://allseeing-i.com/ASIHTTPRequest/How-to-use

Here is a detailed Getting Started Guide.

Now, we need to use it in our own project.

I. Use asihttprequest in the project

1. Copy the source file to the project.

Asihttprequest is an open-source project. To use it, directly copy the project source files to your project, including the following files (all files under classes and all files under external/reachability ):

  • Asihttprequestconfig. h
  • Asihttprequestdelegate. h
  • Asiprogressdelegate. h
  • Asicachedelegate. h
  • Asihttprequest. h
  • Asihttprequest. m
  • Asidatacompressor. h
  • Asidatacompressor. m
  • Asidatadecompressor. h
  • Asidatadecompressor. m
  • Asiformdatarequest. h
  • Asiinputstream. h
  • Asiinputstream. m
  • Asiformdatarequest. m
  • Asinetworkqueue. h
  • Asinetworkqueue. m
  • Asidownloadcache. h
  • Asidownloadcache. m

For iPhone, you also need to copy the following files:

  • Asiauthenticationdialog. h
  • Asiauthenticationdialog. m
  • Reachability. H (external/reachability directory)
  • Reachability. m (external/reachability directory)

2. Add a dependency Library

Asihttprequest depends on the following five frameworks or libraries:

Cfnetwork, systemconfiguration, mobilecoreservices, CoreGraphics, and libz1.2.3.

Add the above libraries and frameworks to the target's linked libraries in sequence.

Ii. Simple synchronous request example

Create an iOS project and add the required source files and linked libraries.

Add a uiview and a uibutton to mainwindow. XIB, add the corresponding exit to the delegate, and connect to the IB.

Write the touch up inside code of the button and connect it to uibutton:

-(Ibaction) gourl {

Nsurl * url = [nsurl urlwithstring: @ "http: // localhost/interface/getdept"];

// Construct the asihttprequest object

Asihttprequest * request = [asihttprequest requestwithurl: url];

// Start synchronization request

[Request startsynchronous];

Nserror * error = [Request Error];

Assert (! Error );

// If the request is successful, return response

Nsstring * response = [Request responsestring];

Nslog (@ "% @", response );

}

Do not forget to import asihttprequest: # import "asihttprequest. H" in the appropriate places"

Save the changes made in IB and xcode, and compile with notepad + B.

Iii. Simple asynchronous request example

Modify the above Code:

-(Ibaction) gourl {

Nsurl * url = [nsurl urlwithstring: @ "http: // localhost/interface/getdept"];

Asihttprequest * request = [asihttprequest requestwithurl: url];

// Set the delegate and delegate the asynchronous request Method

[Request setdelegate: Self];

// Start asynchronous request

[Request startasynchronous];

}

And implement a series of delegate methods:

// The request ends and the response data is obtained.

-(Void) requestfinished :( asihttprequest *) Request

{

Nsstring * responsestring = [Request responsestring]; // For binary data, use: nsdata * responsedata = [Request responsedata];

Nslog (@ "% @", responsestring );

Button. Enabled = yes;

}

// Request failed, get Error

-(Void) requestfailed :( asihttprequest *) Request

{

Nserror * error = [Request Error];

Nslog (@ "% @", error. userinfo );

Button. Enabled = yes;

}

Starting from OS X 10.6 and iOS 4.0, the block syntax is supported. You can also use the block syntax to call asihttprequest:

-(Ibaction) gourl {

Nsurl * url = [nsurl urlwithstring: @ "http: // localhost/interface/getdept"];

_ Block asihttprequest * request = [asihttprequest requestwithurl: url];

// Asihttprequest supports the block Syntax of IOS 4.0. You can define the delegate method into the block.

[Request setcompletionblock: ^ {

// When the request response ends, responsestring is returned.

Nsstring * responsestring = [Request responsestring]; // For binary data, use nsdata to return nsdata * responsedata = [Request responsedata];

Nslog (@ "% @", responsestring );

}];

[Request setfailedblock: ^ {

// If the request response fails, an error message is returned.

Nserror * error = [Request Error];

Nslog (@ "error: % @", [error userinfo]);

}];

[Request startasynchronous];

}

If you are not familiar with the block Syntax of O-C, refer to another blog by the author
Block programming guide, or apple reference library.

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.