The ASIHttpRequest function is used to load network data and data.
To use the third library of ASIHttpRequest, You need to configure
II,
The data upload function uses ASIFromDataRequest (binary and string can be uploaded to the server)
The following is a test
// Create a project and import it to a third-party library in AppDelegate. h # import <UIKit/UIKit. h> # import "ASIFormDataRequest. h "@ interface AppDelegate: UIResponder <UIApplicationDelegate, ASIHTTPRequestDelegate >{// declare the data request member variable ASIFormDataRequest * _ request ;}@ property (strong, nonatomic) UIWindow * window; @ end
// In AppDelegate. code written in m // load data through the POST request NSURL * url = [NSURL URLWithString: @ "http: // 10.0.8.8/sns/my/login. php "]; // instance into object _ request = [[ASIFormDataRequest alloc] initWithURL: url]; // set the request method [_ request setRequestMethod: @" post "]; // Add the parameter [_ request addPostValue: @ "aa_Zx" forKey: @ "username"]; [_ request addPostValue: @ "ffff" forKey: @ "password"]; // sets proxy callback _ request. delegate = self; // request data [_ request startAsynchronous]; // asynchronous request, multithreading
# Param-mark ASIHttpRequest-(void) requestFinished :( ASIHTTPRequest *) request {NSDictionary * dict = [NSJSONSerialization JSONObjectWithData: request. responseData options: NSJSONReadingMutableContainers error: nil]; NSLog (@ "% @", dict) ;}- (void) requestFailed :( ASIHTTPRequest *) request {NSLog (@ "request failed ");}
3. Attach network data and ASIHttpRequest (data in all formats can be loaded)
-(Void) startLoadWebData :( NSString *) strURL {// instantiate ASIHttpRequest object NSURL * url = [NSURL URLWithString: strURL]; _ request = [[ASIHTTPRequest alloc] initWithURL: url]; _ requent. delegate = self; // start to load data [_ requenst startAsynchronous];} # Call this method when pragma-mark ASIHttpRequestDelegate // when the request is successful-(void) requestFinished :( ASIHTTPRequest *) request {NSDictionary * dict = [NSJSONSerialization JSONObjectWithData: request. responseDataoptions: NSJSONReadingMutableContainers error: nil]; // The proxy receives data [self. delegate reciveData: [dict objectForKey: @ "application"];} // call this method when network data fails to be requested-(void) requestFailed :( ASIHTTPRequest *) request {NSLog (@ "failed ");}