iOS中ASI

ASI-HTTP-Request简介

简称ASI。

使用iOS SDK中的HTTP网络请求API比较复杂,调用很繁琐。ASI是简单易用的,它封装了CFNetwork API。使得与web服务通信变得更简单。它是使用Objective-C写的,可以在MAC OS X和iOS应用中使用


ASI地址 :https://github.com/pokeb/asi-http-request



ASI第三方库配置过程   

(非ARC,基于底层的CFNetwork框架;已经停止更新)

1.导入ASI的源文件


2.导入五个框架点击 项目 


                CFNetwork.framework

                 SystemConfiguration.framework

                 MobileCoreServices.framework

                 libxml2.dylib

                 libz.dylib

选中 targers —》》bulid phases —》》


link binary with libraries 点击+号


导入需要添加的包


   3.导入头文件 即可使用

ASI异步get请求

1 首先创建一个url 

NSURL *url =[NSURL URLWithString:@"http://www.baidu.com"];


2 创建请求

</pre><pre name="code" class="objc"><strong><span style="color:#333333;">ASIHTTPRequest *request =[ASIHTTPRequest requestWithURL:url]</span></strong>


;

3设置代理

request.delegate =self;此时需要在.h文件中导入ASIHTTPRequestDelegate.h文件

4开启异步请求


[request startAsynchronous];


以下是ASIhttp代理的方法

//获得响应头
-(void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders
{
      //获得请求状态码
     // request.responseStatusCode
    
    //遍历响应头key
    for (NSString *str in [responseHeaders allKeys]) {
        NSLog(@"%@",str);
        }
}
//请求完成  
-(void)requestFinished:(ASIHTTPRequest *)request
{
    
     
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    NSURL *url=[NSURL URLWithString:@"http://www.baidu.com"];
    
    ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url];
    
    
    request.delegate=self;
    
    [request startAsynchronous];
    return YES;
}
-(void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders
{
    NSLog(@"%d",request.responseStatusCode);
    
    
    for (NSString *str in [responseHeaders allKeys]) {
        
        NSLog(@"%@",str);
        
    }
    

}
-(void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data
{
    

}
-(void)requestFinished:(ASIHTTPRequest *)request
{
    

}



输出结果如下


ASI同步get请求

1 首先创建一个url 
NSURL *url =[NSURL URLWithString:@"http://www.baidu.com"];
2 创建请求
ASIHTTPRequest *request =[ASIHTTPRequest requestWithURL:url];
3 开始请求

[request startSynchronous];
4
获得响应数据                request.responseData;
获得响应数据转化的字符串              request.responseString
获得响应头内容              request.responseHeaders
获得服务器响应错误           request.error

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    NSURL *url=[NSURL URLWithString:@"http://www.baidu.com"];
    ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url];
    
    [request startSynchronous];
    
    //获得响应头状态码
    NSLog(@"%d",request.responseStatusCode);
    
    NSLog(@"%@",request.responseString);
    
    
    
    return YES;
}




输出结果


asi 同步POST 请求


//服务器地址
    NSURL *url=[NSURL URLWithString:@"http://localhost:8080/MyFirstWeb/NewServlet"];
    //asi 同步POST 请求
    ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
    //设置请求体
    [request addPostValue:@"zhangsan" forKey:@"name"];
    [request addPostValue:@"123" forKey:@"password"];
    
    
    request.delegate=self;
    [request startSynchronous];
    //服务器的响应
    NSLog(@"%@",request.responseString);


注意:此时导入的是ASIFormDataRequest.h

输出结果



asi 异步POST 请求


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    NSURL *url=[NSURL URLWithString:@"http://localhost:8080/MyFirstWeb/NewServlet"];
    
    ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
    
    request.delegate=self;
    //请求体
    [request addPostValue:@"zhangsan" forKey:@"name"];
    
    [request addPostValue:@"123" forKey:@"password"];
    
    [request startAsynchronous];
    
    
    
    
    
    return YES;
}
//获得请求头
-(void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders
{

    NSLog(@"%d",request.responseStatusCode);
    
}
//请求完成
-(void)requestFinished:(ASIHTTPRequest *)request
{

    NSLog(@"%@",request.responseString);

}
//请求失败
-(void)requestFailed:(ASIHTTPRequest *)request
{
    NSLog(@"%@",request.error);

}


输出结果



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值