设为首页 加入收藏

TOP

ios开发网络篇—Get请求和Post请求 - 转(一)
2019-08-26 06:55:10 】 浏览:47
Tags:ios 开发 网络 Get 求和 Post 请求

简单说明:建议提交用户的隐私数据一定要使用Post请求 
相对Post请求而言,Get请求的所有参数都直接暴露在URL中,请求的URL一般会记录在服务器的访问日志中,而服务器的访问日志是黑客攻击的重点对象之一 
用户的隐私数据如登录密码,银行帐号等

示例代码

#define CURRENT_SCREEN_WIDTH     [UIScreen mainScreen].bounds.size.width
#define CURRENT_SCREEN_HEIGHT     ([UIScreen mainScreen].bounds.size.height - 64)
#define BUTTON_WIDTH     80
#define BUTTON_HEIGHT    40

@interface ViewController ()
//GET 请求
@property(nonatomic,strong) UIButton *getButton;
//POST 请求
@property(nonatomic,strong) UIButton *postButton;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _getButton = [[UIButton alloc] initWithFrame:CGRectMake(CURRENT_SCREEN_WIDTH/2 - BUTTON_WIDTH/2,
                                                             CURRENT_SCREEN_HEIGHT/2 - BUTTON_HEIGHT,
                                                             BUTTON_WIDTH,
                                                             BUTTON_HEIGHT)];
    [_getButton setTitle:@"GET 请求" forState:UIControlStateNormal];
    [_getButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_getButton addTarget:self
                    action:@selector(getClick)
          forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_getButton];
    
    _postButton = [[UIButton alloc] initWithFrame:CGRectMake(CURRENT_SCREEN_WIDTH/2 - BUTTON_WIDTH/2,
                                                             _getButton.frame.origin.y + _getButton.frame.size.height + 60,
                                                             BUTTON_WIDTH,
                                                             BUTTON_HEIGHT)];
    [_postButton setTitle:@"POST 请求" forState:UIControlStateNormal];
    [_postButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_postButton addTarget:self
                    action:@selector(postClick)
          forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_postButton];
}

/* get 请求 */
-(void)getClick{
    //请求 URL
    NSString* urlStr = [NSString stringWithFormat:@"https://m.che168.com/beijing/?pvareaid=%d",110100];
    //封装成 NSURL
    NSURL* url = [NSURL URLWithString:urlStr];

    //初始化 请求对象
    NSURLRequest* request = [[NSURLRequest alloc] initWithURL:url];
    //也可以这样初始化对象
    //NSURLRequest* request = [NSURLRequest requestWithURL:url];
    
    //发送请求  默认为 GET 请求
    //1 、获得会话对象
    NSURLSession *session = [NSURLSession sharedSession];
    // 2、第一个参数:请求对象
    //      第二个参数:completionHandler回调(请求完成【成功|失败】的回调)
    //      data:响应体信息(期望的数据)
    //      response:响应头信息,主要是对服务器端的描述
    //      error:错误信息,如果请求失败,则error有值
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if(!error){
            NSLog(@"请求加载成功。。。");
            //说明:(此处返回的数据是JSON格式的,因此使用NSJSONSerialization进行反序列化处理)
            // NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
            //如果是字符串则直接取出
            NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"GET 请求返回的结果是:%@",[str substringToIndex: 300]);
        }
    }];
    //执行任务
    [dataTask resume];
    
    /* ------------ ios9 之前请求方法,之后改成 NSURLSession 请求  --------------
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if(!connectionError){
            NSLog(@"加载成功。。。");
            NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@&qu
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇iOS url出现特殊字符处理 -- stri.. 下一篇iOS开发网络篇—发送GET和POST请..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目