get和post的区别

/*

 HTTP(Hypertext Transfer Protocol) 超文本传输协议:是互联网是应用最为广泛的一种网络协议  所有的WWW网页都必须遵守这个协议

 http是用于WWW(万维网)浏览传输数据的一个协议

 http://服务器地址 资源位置

 

 Ip协议对应网络层 , TCP协议对应传输层, 而HTTP协议对应应用层(识别数据内容)

 

 http协议的作用

 (1)规定客户端和服务器之间的数据传输格式

 (2)让客户端和服务器能有效地进行数据沟通

 

 使用HTTP的好处:

   (1)简单快速 :因为HTTP的协议简单,所以HTTP服务器程序规模小 因而通信速度快

    (2)灵活 :HTTP允许传输任意类型的数据

    (3)HTTP是非持续链接 限制每次链接只处理一个请求 ,服务器对客户端的请求做出响应,马上断开连接,这种方式可以节省传输时间

 

 HTTP的通信过程

   (1)请求:客户端向服务器索要数据

    (2)响应:服务器返回客户端相应的数据

 

 HTTP的请求方法:get  post

 get  会把请求的内容 拼接到链接 地址里面(默认的数据请求是get)

 WWW.baidu.com/user/login?username = 刘夏,psw = 123

 get特征:1.浏览器和服务器对URL长度有限制,因此在URL后面附带的参数是有限制的,通常不能超过1kb

       2.会把请求的信息暴露在接口里面

 

 post 参数全部放在请求体中 这样就保证了  数据安全  且没有具体的长度限制(唯一的限制 就是 服务器的承受能力)

 

 

 选择get和post的建议

   (1)如果要传递大量数据,比如文件上传,只能用post请求

   (2)get的安全性比post要差一些,如果包含机密\敏感信息,建议用post

   (3)如果仅仅是索取数据(数据查询),建议使用get

    (4)如果是增加、修改、删除数据,建议使用post

 

 URL(Uniform Resource Locator)统一资源定位符 通过1个URL,能找到互联网上唯一的1个资源

 

 

 */

 

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blackColor];

    [self loadData6];

    

}

 

//通过URL 获得URL里面的内容(字符串)

- (void)loadData1{

//    把 字符串 转成 NSURL

//    NSUTF8StringEncoding 一个编码格式

    NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];

    NSString *content = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

    NSLog(@"%@",content);

}

//通过URL 获得URL里面的data

- (void)loadData2{

    

     NSURL *url = [NSURL URLWithString:@"http://preview.quanjing.com/is_rm001/is0997q92.jpg"];

    NSData *data = [NSData dataWithContentsOfURL:url];

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.frame];

    imageView.contentMode = UIViewContentModeScaleToFill;

    imageView.image = [UIImage imageWithData:data];

    [self.view addSubview:imageView];

}

 

//网络请求有 同步请求 和 异步请求

//同步请求:等所有操作 完全执行完毕 才会继续执行

//         弊端; 会遇到 假死的情况 (只要请求的操作 没有执行完毕 就不会去响应同一线程里的任何事件)

 

//异步请求:是在程序运行的时候 会利用空闲的时间 去执行里面的操作 不会影响到 同一线程里 的 其他操作

 

 

//同步请求

- (void)loadData3{

    NSURL *url = [NSURL URLWithString:@"http://preview.quanjing.com/is_rm001/is0997q92.jpg"];

//    实例化 请求对象 里面携带着 请求的地址

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

//NSURLConnection发送请求的类   data 是服务器返回给我们的数据

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

    

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.frame];

    imageView.contentMode = UIViewContentModeScaleToFill;

    imageView.image = [UIImage imageWithData:data];

    [self.view addSubview:imageView];

 

    

}

 

//异步请求

- (void)loadData4{

    

    

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.frame];

    imageView.contentMode = UIViewContentModeScaleToFill;

//    imageView.image = [UIImage imageWithData:data];

    [self.view addSubview:imageView];

    

    

    NSURL *url = [NSURL URLWithString:@"http://preview.quanjing.com/is_rm001/is0997q92.jpg"];

    //    实例化 请求对象 里面携带着 请求的地址

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    

//    需要 通过 链接 异步发送请求

//    NSOperationQueue 线程

    NSOperationQueue *queue = [[NSOperationQueue alloc]init];

//    发送一个异步请求 在queue 这个线程里面去执行

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

//        response 服务器 回应的 内容 (回应状态的code 以及error)

//        data 回应给 客服端 需要的数据

        NSLog(@"%@",response);

        imageView.image = [UIImage imageWithData:data];

    }];

    

}

 

//get 把传输的数据 放在连接的地址里面

- (void)loadData5{

    NSString *interFaceString = @"http://apis.baidu.com/showapi_open_bus/mobile/find";

    NSString *requestContentString = @"num=13370116152";

    NSString *urlString = [NSString stringWithFormat:@"%@?%@",interFaceString,requestContentString];

//    把连接地址字符串 转成 NSUTF8StringEncoding。连接地址字符串可以用时就不必转换了

    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

//    可变请求 可以添加 请求方式 以及请求的请求头 或者 更多

//    timeoutInterval 允许请求所需的时间(秒) 超过时间 不再发送这个请求

//    cachePolicy 缓存内容的方式

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];

//    HTTPMethod 指定HTTP的请求方式

    request.HTTPMethod = @"GET";

    NSString *apiKey = @"e7f5ac9e7c42a6c8cb125ee1d7e8779e";

//    把apikey 发送给服务器 指定的请求头 位置

//    forHTTPHeaderField 需要的key是 服务器指定的key

    [request addValue:apiKey forHTTPHeaderField:@"apikey"];

    

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

        NSLog(@"%@",request);

        

//        解析 json 文件

//        把data 转换成json文件

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

        NSLog(@"%@ %@",info[@"showapi_res_body"][@"city"],info[@"showapi_res_body"][@"name"]);

    }];

    

}

//post请求

- (void)loadData6{

    NSURL *url = [NSURL URLWithString:@"http://www.weihuok.com/customer2/GetService"];

//    请求参数

//    PlatformType 设备类型 3表示IOS设备

    NSDictionary *dic = @{@"PlatformType":@"3"};

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];

//    设置HTTP的请求方式

    request.HTTPMethod = @"POST";

//    设置请求的参数

//    dataUsingEncoding 把字符串 转成data

//    HTTPBody 需要的是data类型的数据

    request.HTTPBody = [[NSString stringWithFormat:@"%@",dic]dataUsingEncoding:NSUTF8StringEncoding];

    

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

        

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

        NSLog(@"===%@",info);

        

    }];

    

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

 

转载于:https://www.cnblogs.com/liYongJun0526/p/4869828.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值