首页 > 代码库 > IOS中get同步异步请求与post同步异步请求
IOS中get同步异步请求与post同步异步请求
demo
// Created by apple on 15/1/6.
// Copyright (c) 2015年 huweibin. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UITextView *textView;
@property(nonatomic,copy)NSString *BASE_URL;
@property(nonatomic,copy)NSString *BASE_URL1_PARAM;
@property(nonatomic,strong)NSMutableData *mutableData;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark - get同步
- (IBAction)getSyncButtonAction:(UIButton *)sender
{
NSString * BASE_URL= @"www.baidu.com";
//1.准备URL地址
NSURL *url = [NSURLURLWithString:BASE_URL];
//2.准备请求对象
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
//2.1设置请求方式
[request setHTTPMethod:@"GET"];
//3.准备返回结果
NSURLResponse *response = nil;
NSError *error = nil;
//4.创建链接对象,并发送请求,并获取结果(需要的数据)
NSData *data =http://www.mamicode.com/ [NSURLConnectionsendSynchronousRequest:request returningResponse:&response error:&error];
//5.打印获取到的一些信息
NSLog(@"结果类型:%@",response.MIMEType);
NSLog(@"请求的网址:%@",response.URL);
NSLog(@"结果长度:%lld",response.expectedContentLength);
NSLog(@"请求到的结果:%@",data);
//6.解析文件
NSDictionary *dict = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingAllowFragmentserror:nil];
//7.显示在textView里
self.textView.text = [NSStringstringWithFormat:@"%@",dict];
}
#pragma mark - get异步
- (IBAction)getAsyncButtonAction:(UIButton *)sender
{
//1.准备url地址
NSURL *url = [NSURLURLWithString:_BASE_URL];
//2.创建请求对象
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
//3.创建链接对象,发送请求
[NSURLConnectionconnectionWithRequest:request delegate:self];
}
#pragma mark - POST同步
- (IBAction)postSyncButtonAction:(UIButton *)sender
{
//1.准备网址
NSURL *url = [NSURLURLWithString:_BASE_URL];
//2.准备请求对象
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
//2.1设置请求方式
[request setHTTPMethod:@"POST"];
//2.2设置请求参数
#warning 设置请求参数,需要的是NSData类型
NSData *param = [_BASE_URL1_PARAMdataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:param];
//3.创建链接对象,并发送请求,获取结果
NSData *data = http://www.mamicode.com/[NSURLConnectionsendSynchronousRequest:request returningResponse:nilerror:nil];
//4.解析
NSDictionary *dict = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingAllowFragmentserror:nil];
//5.显示
self.textView.text = [NSStringstringWithFormat:@"%@",dict];
}
#pragma mark - POST异步
- (IBAction)postAsyncButtonAction:(UIButton *)sender
{
__block ViewController *weakSelf =self;
//1.准备地址
NSURL *url = [NSURLURLWithString:_BASE_URL];
//2.创建请求对象,并设置请求方法和参数
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[_BASE_URL1_PARAMdataUsingEncoding:NSUTF8StringEncoding]];
//3.创建链接对象,发送请求,在block内部完成分析
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueuenew] completionHandler:^(NSURLResponse *response,NSData *data, NSError *connectionError) {
//NSLog(@"%@",data);
//4.解析
NSDictionary *dict = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingAllowFragmentserror:nil];
//5.回到主线程,进行更新页面
dispatch_sync(dispatch_get_main_queue(), ^{
weakSelf.textView.text = [NSStringstringWithFormat:@"%@",dict];
});
}];
}
#pragma mark - 清除
- (IBAction)clearButtonAction:(UIButton *)sender
{
_textView.text =nil;
}
#pragma mark - 实现协议方法
#pragma mark 开始接收请求结果
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//初始化
self.mutableData = [NSMutableDatadata];
}
#pragma mark - 接收数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//拼接接收到的数据
[self.mutableDataappendData:data];
}
#pragma makr - 接收完毕
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//解析
NSDictionary *dict = [NSJSONSerializationJSONObjectWithData:_mutableDataoptions:NSJSONReadingAllowFragmentserror:nil];
_textView.text = [NSStringstringWithFormat:@"%@",dict];
}
#pragma mark - 接收错误
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
}- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
IOS中get同步异步请求与post同步异步请求