IOS 网络浅析-(四 get&post)

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);
        
    }];
    
}
复制代码

 

分类:  IOS 网络
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值