ASIHTTPRequest-插件的使用

链接地址:http://blog.sina.com.cn/s/blog_7b9d64af0101e5uf.html

一、什么是ASIHTTPRequest
 
ASIHTTPRequest 插件是一个开源的第三方网络请求相关的插件!目前大多数关于网络请求的操作,我都使用它,简单,适用,方便,高效,给力!!!
 
二、集成ASIHTTPRequest
 
1、下载ASIHTTPRequest 插件包。
下载地址:https://github.com/pokeb/asi-http-request/tree
2、加入到项目中。
3、在需要使用ASIHTTPRequest相关功能的页面中,加入头文件(.h文件)的引用。

#import "ASIHTTPRequest.h"

#import "ASIFormDataRequest.h"// POST 方式需要引入

4、引入相应的框架包。

 CFNetwork、SystemConfiguration、MobileCoreServices、 libz、 libxml2

 
三、Get方式请求Demo
 
还是直接贴代码吧!!!
 

-(void) Request_GET

{

    NSString* urlString = [NSString stringWithFormat:@"Your_URL"];

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURLURLWithString:urlString]];

    [request setDefaultResponseEncoding:NSUTF8StringEncoding];

    [request setResponseEncoding:NSUTF8StringEncoding];

    [request setTimeOutSeconds:15.0f];//5s超时

    [request setCompletionBlock:^{

    NSString* hexString = [request responseString];

            

        

    }];

    

    [request setFailedBlock:^{

        

    }];

    

[request startSynchronous];// 同步请求

    //    [request startAsynchronous];// 异步请求

}

 
四、POST方式请求Demo
 

- (void) Request_POST{

   NSString *appid = @"22222";

    NSString* urlString = [NSStringstringWithFormat:@"https://graph.qq.com/t/add_t"];

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURLURLWithString:urlString]];

    [request setPostValue:[_tencentOAuth accessToken] forKey:@"access_token"];

    [request setPostValue:[_tencentOAuth openId] forKey:@"openid"];

    [request setPostValue:appid forKey:@"oauth_consumer_key"];

    [request setPostValue:@"json" forKey:@"format"];

    [request setPostValue:@"You_Content" forKey:@"content"];

    

    [request setDelegate:self];

    [request setTimeOutSeconds:30.0f];//5s超时

    [request setCompletionBlock:^{

        NSLog(@"sucess");

        NSString* hexString = [request responseString];

        

        NSLog(@"%@",hexString);

    

    

    }];

    [request setFailedBlock:^{

         NSLog(@"Failed");

        

    }];

    

    [request startAsynchronous];

}

 
先mark 到这里吧,以后用得到的地方,在慢慢深入!
 
希望对你有所帮助!
如果一件事情你觉得难的完不成,你可以把它分为若干步,并不断寻找合适的方法。最后你发现你会是个超人。不要给自己找麻烦,但遇到麻烦绝不怕,更不要退缩。 电工查找电路不通点的最快方法是:分段诊断排除,快速定位。你有什么启示吗? 求知若饥,虚心若愚。 当你对一个事情掌控不足的时候,你需要做的就是“梳理”,并制定相应的规章制度,并使资源各司其职。
原文地址:https://www.cnblogs.com/wvqusrtg/p/4831206.html