码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发ASIHttpRequest发送请求

时间:2015-02-04 18:17:40      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:

#pragma mark - 同步请求
- (void)sync
{
    // 1.创建请求对象
    NSURL *url = [NSURL URLWithString:@"http://192.168.x.x:8080/login?username=123&pwd=123"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    request.timeOutSeconds = 10;
    
    // 2.发送请求
    [request startSynchronous];
    
    // 3.检测服务器返回的结果
    if (request.error) { // 请求出错(比如超时)
        NSLog(@"请求出错---%@", request.error);
    } else { // 请求成功
        NSLog(@"%d %@", request.responseStatusCode, request.responseStatusMessage);
        //        NSLog(@"%d", request.responseData.length);
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:request.responseData options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"%@", dict);
        //        NSLog(@"%@", request.responseString);
    }
}
#pragma mark - 异步请求--设置代理
- (void)async
{
    // 1.创建请求对象
    NSURL *url = [NSURL URLWithString:@"http://192.168.x.x:8080/login?username=123&pwd=123"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    request.timeOutSeconds = 10;
    
//    NSURL *url2 = [NSURL URLWithString:@"http://192.168.1.200:8080/MJServer/video"];
//    ASIHTTPRequest *request2 = [ASIHTTPRequest requestWithURL:url2];
    
    
    // 2.设置代理
    request.delegate = self;
//    request2.delegate = self;
    
    [request setDidStartSelector:@selector(start)];
    
    // 3.发送请求
    [request startAsynchronous];
//    [request2 startAsynchronous];
}

- (void)start
{
    NSLog(@"start--------");
}

#pragma mark 代理方法
- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSLog(@"requestFinished----");
//    request.responseData : 服务器返回的全部数据
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSLog(@"requestFailed----");
}

- (void)requestStarted:(ASIHTTPRequest *)request
{
    NSLog(@"requestStarted----");
}

- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data
{
    NSLog(@"didReceiveData----");
}
#pragma mark - 异步请求--block
- (void)async
{
    // 1.创建请求对象
    NSURL *url = [NSURL URLWithString:@"http://192.168.x.x:8080/login?username=123&pwd=123"];
    self.request = [ASIHTTPRequest requestWithURL:url];
    self.request.timeOutSeconds = 10;
    
    // 2.设置block
    [self.request setStartedBlock:^{
        NSLog(@"setStartedBlock ----");
    }];
    
    [self.request setDataReceivedBlock:^(NSData *data) {
        NSLog(@"setDataReceivedBlock ----");
    }];
    
    [self.request setCompletionBlock:^{
        NSLog(@"setCompletionBlock ----");
    }];
    
//    self.request.delegate = self;
    
    // 3.发送请求
    [self.request startAsynchronous];
    
    
//    NSURL *url2 = [NSURL URLWithString:@"http://192.168.x.x:8080/"];
//    ASIHTTPRequest *request2 = [ASIHTTPRequest requestWithURL:url2];
//    [request2 setCompletionBlock:^{
//        
//    }];
//    
//    [request2 setDataReceivedBlock:^(NSData *data) {
//        
//    }];
//    [request2 startAsynchronous];
}

 post请求

#pragma mark - post请求
- (void)post2
{
    // 1.创建请求对象
    NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/MJServer/login"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    
    // 2.添加请求参数(请求体中的参数)
    [request setPostValue:@"123" forKey:@"username"];
    [request setPostValue:@"999" forKey:@"pwd"];
    
    [request setCompletionBlock:^{
        NSLog(@"---请求完毕");
    }];
    
    // 3.发送请求
    [request startAsynchronous];
}

- (void)post
{
    // 1.创建请求对象
    NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/MJServer/login"];
    self.request = [ASIHTTPRequest requestWithURL:url];
    self.request.timeOutSeconds = 10;
    
    // 设置请求方法
    self.request.requestMethod = @"POST";
    
    // 设置请求体
    [self.request appendPostData:[@"username=123&pwd=123" dataUsingEncoding:NSUTF8StringEncoding]];
//    [self.request setPostLength:self.request.postBody.length];
//    [self.request addRequestHeader:[NSString stringWithFormat:@"%d", self.request.postBody.length] value:@"Content-Length"];
//    [self.request addRequestHeader:@"application/x-www-form-urlencoded" value:@"Content-Type"];
    
    // 2.发送请求
    [self.request startAsynchronous];
}

 

iOS开发ASIHttpRequest发送请求

标签:

原文地址:http://www.cnblogs.com/jdongcs/p/4272815.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!