您的位置:首页 > 理论基础 > 计算机网络

网络发送同步请求——(开发中一般不会使用,但是还是了解的好)

2015-10-26 14:31 661 查看
新建工程代码如下:

//
//  ViewController.m
//  异步连接参数解析
//
//  Created by apple on 15/10/26.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

// 1 url
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/demo.json"];

// 2. 建立请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];

// 3. 建立连接Connection,发送同步请求到服务器
/**
参数:
SynchronousRequest:发送同步请求
returningResponse:服务器响应的地址
error:错误信息的地址
*/
NSURLResponse *response = nil;
NSData *data =   [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSLog(@"reponse= %@", response);
NSLog(@"%@——%@",  [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding], [NSThread currentThread]);
}
@end


运行结果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: