中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

IOS中g(shù)et同步異步請(qǐng)求與post同步異步請(qǐng)求

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用
 #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 {

    [super viewDidLoad];

    

    

    // 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.準(zhǔn)備URL地址

    NSURL *url = [NSURL URLWithString:BASE_URL];

    

    //2.準(zhǔn)備請(qǐng)求對(duì)象

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    

    //2.1設(shè)置請(qǐng)求方式

    [request setHTTPMethod:@"GET"];

    

    //3.準(zhǔn)備返回結(jié)果

    NSURLResponse *response = nil;

    NSError *error = nil;

    

    //4.創(chuàng)建鏈接對(duì)象,并發(fā)送請(qǐng)求,并獲取結(jié)果(需要的數(shù)據(jù))

    NSData *data =  [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    

    //5.打印獲取到的一些信息

    NSLog(@"結(jié)果類型:%@",response.MIMEType);

    NSLog(@"請(qǐng)求的網(wǎng)址:%@",response.URL);

    NSLog(@"結(jié)果長(zhǎng)度:%lld",response.expectedContentLength);

    NSLog(@"請(qǐng)求到的結(jié)果:%@",data);

    

    //6.解析文件

    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

    

    //7.顯示在textView里

    self.textView.text = [NSString stringWithFormat:@"%@",dict];

    

}


#pragma mark - get異步

- (IBAction)getAsyncButtonAction:(UIButton *)sender

{

    //1.準(zhǔn)備url地址

    NSURL *url = [NSURL URLWithString:_BASE_URL];

    //2.創(chuàng)建請(qǐng)求對(duì)象

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    //3.創(chuàng)建鏈接對(duì)象,發(fā)送請(qǐng)求

    [NSURLConnection connectionWithRequest:request delegate:self];

    

}



#pragma mark - POST同步

- (IBAction)postSyncButtonAction:(UIButton *)sender

{

    //1.準(zhǔn)備網(wǎng)址

    NSURL *url = [NSURL URLWithString:_BASE_URL];

    

    //2.準(zhǔn)備請(qǐng)求對(duì)象

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    

    //2.1設(shè)置請(qǐng)求方式

    [request setHTTPMethod:@"POST"];

    

    //2.2設(shè)置請(qǐng)求參數(shù)

#warning 設(shè)置請(qǐng)求參數(shù),需要的是NSData類型

    NSData *param = [_BASE_URL1_PARAM dataUsingEncoding:NSUTF8StringEncoding];

    [request setHTTPBody:param];

    

    //3.創(chuàng)建鏈接對(duì)象,并發(fā)送請(qǐng)求,獲取結(jié)果

    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    

    //4.解析

    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

    

    //5.顯示

    self.textView.text = [NSString stringWithFormat:@"%@",dict];

}


#pragma mark - POST異步

- (IBAction)postAsyncButtonAction:(UIButton *)sender

{

    __block ViewController *weakSelf = self;

    

    //1.準(zhǔn)備地址

    NSURL *url = [NSURL URLWithString:_BASE_URL];

    //2.創(chuàng)建請(qǐng)求對(duì)象,并設(shè)置請(qǐng)求方法和參數(shù)

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    [request setHTTPMethod:@"POST"];

    [request setHTTPBody:[_BASE_URL1_PARAM dataUsingEncoding:NSUTF8StringEncoding]];

    

    //3.創(chuàng)建鏈接對(duì)象,發(fā)送請(qǐng)求,在block內(nèi)部完成分析

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue new]  completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        //NSLog(@"%@",data);

        

        //4.解析

        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

        

        //5.回到主線程,進(jìn)行更新頁面

        dispatch_sync(dispatch_get_main_queue(), ^{

            weakSelf.textView.text = [NSString stringWithFormat:@"%@",dict];

        });

        

    }];

    

    

    

}



#pragma mark - 清除

- (IBAction)clearButtonAction:(UIButton *)sender

{

    _textView.text = nil;

}





#pragma mark - 實(shí)現(xiàn)協(xié)議方法

#pragma mark 開始接收請(qǐng)求結(jié)果

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{

    //初始化

    self.mutableData = [NSMutableData data];

}


#pragma mark - 接收數(shù)據(jù)

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

    //拼接接收到的數(shù)據(jù)

    [self.mutableData appendData:data];

    

}


#pragma makr - 接收完畢

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    //解析

    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:_mutableData options:NSJSONReadingAllowFragments error:nil];

    _textView.text = [NSString stringWithFormat:@"%@",dict];

}



#pragma mark - 接收錯(cuò)誤

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{

    

}- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


標(biāo)簽: isp

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:PHP 對(duì) png 圖像進(jìn)行縮放,支持透明背景

下一篇:發(fā)送郵件Golang代碼