zoukankan      html  css  js  c++  java
  • IOS 网络浅析-(四 get&post)

    网络请求默认是get

    网络请求有很多种:GET查  POST改  PUT增  DELETE删 HEAD

    在平时开发中主要用的 是 get 和 post.

    get 获得数据 (获取用户信息)

    get 请求是没有长度限制的,真正的长度限制是浏览器做的,限制长度一般2k

    get 请求是有缓存的,get 有幂等的算法

    get  http://localhost/login.php?username=xubaoaichiyu&password=123456

    请求参数暴露在url里

    get请求参数格式:

    ?后是请求参数

    参数名 = 参数值  

    & 连接两个参数的

    post 添加,修改数据 (上传或修改用户信息)

    post 请求是没有缓存的

    http://localhost/login.php

    post 也没有长度限制,一般控制2M以内

    post 请求参数不会暴漏在外面 ,不会暴漏敏感信息

    请求是有:请求头header,请求体boby(post参数是放在请求体里的)

    get代码如下:

    //
    //  ViewController.m
    //  CX-get
    //
    //  Created by ma c on 16/3/17.
    //  Copyright © 2016年 xubaoaichiyu. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        //使用get请求,获取接口
        
        NSString * String = @"http://localhost/login.php";
        
        //拼接参数
        NSString * urlString = [NSString stringWithFormat:@"%@?username=xubaoaichiyu&password=123456",String];
        
        //如果有中文进行转码
        
        urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        NSURL * url = [NSURL URLWithString:urlString];
        
        NSURLRequest * request = [[NSURLRequest alloc]initWithURL:url cachePolicy:0 timeoutInterval:15];
        
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
            
            NSString * string = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            
            NSLog(@"%@",string);
            
        }];
    
    }
    
    @end

    post:

    //
    //  ViewController.m
    //  CX-post
    //
    //  Created by ma c on 16/3/17.
    //  Copyright © 2016年 xubaoaichiyu. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        //使用post请求
        //获取接口
        NSString * string = @"http://localhost/login.php";
        
        //中文转码
        string = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        NSURL * url = [NSURL URLWithString:string];
        
        //可变请求
        NSMutableURLRequest * requst = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:0 timeoutInterval:15];
        
        //设置传输方式
        
        requst.HTTPMethod = @"POST";
        
        NSString * bodyString = [NSString stringWithFormat:@"username=xubaoaichiyu&password=123456"];
        
        //设置请求体
        
        requst.HTTPBody = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
        
        [NSURLConnection sendAsynchronousRequest:requst queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
            
            NSString * string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            
            NSLog(@"%@",string);
            
        }];
        
    }
  • 相关阅读:
    PAT (Basic Level) Practice (中文)1076 Wifi密码 (15 分)
    PAT (Basic Level) Practice (中文)1047 编程团体赛 (20 分)
    PAT (Basic Level) Practice (中文)1029 旧键盘 (20 分)
    PAT (Basic Level) Practice (中文)1016 部分A+B (15 分)
    延迟加载
    Js之全局函数
    Js之数组
    前端性能优化
    排序算法小结
    CSS居中总结
  • 原文地址:https://www.cnblogs.com/xubaoaichiyu/p/5289680.html
Copyright © 2011-2022 走看看