发送http请求的方法

在http/1.1 协议中,定义了8种发送http请求的方法

get post options head put delete trace connect patch.

根据http协议的设计初衷,不同的方法对资源有不同的操作方式,

put :增

delete:删

post:改

get:查

最常用的的是get  和  post (实际上get 和 post 都能办到增删改查)

要想使哟过get  和 post  就必须了解一些概念,

1.参数:比如账号和密码的传递。

get  和 post 区别:

get 的本质就是“得”,post的本质就是“给”。

开发中使用最多的是get,但是如果涉及到用户的隐私数据传递,那么一定要使用post。这样可以保护用户的隐私信息。

get 是可以缓存的,post 是没有缓存的。

get的参数都在url里边,post的参数都是被包装过的。

get :

  从服务器拿数据效率高。

  从数学的角度讲,get的结果是“幂等”。

  get请求能够被缓存(由于能幂等,所以就可以缓存)

  在http协议定义中,没有对get请求的数据大小限制,不过因为浏览器不同,一般限制在2k ~8k 之间。

  所有的参数包装在url中,并且服务器的访问日志会记录,不要传递敏感信息。

  参数格式:?在资源路径末尾添加?表示追加参数。

       变量名=变量值,每个变量及值安照“变量名 = 变量值”方式设定,不能包含空格或者中文

       & 多个参数适用&连接

//
//  ViewController.m
//  01-login(登陆)
//
//  Created by jerry on 15/10/8.
//  Copyright (c) 2015年 jerry. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
    /**
     *  GET
     http://127.0.0.1/login.php?username=%@&password=%@
     1.http://127.0.0.1 是主机地址
     2.login.php是服务器负责登陆的脚本(php,java,.net)
     3.?后面是参数,是给服务器传递参数的
       参数的格式
       变量名 = 值
       username = @"zhangsan"
     4.& 如果是多个参数,用&进行连接。
     */
    
    NSString *userName = @"zhangsan";
    NSString *userPwd = @"zhang";
//    地址
    NSString *add =  [NSString stringWithFormat:@"http://127.0.0.1/login.php?username=%@&password=%@",userName,userPwd];
    // url 里面是不可以包含中文、空格、特殊符号等,如果有的话需要进行转译(%号的转译)  username = @"张三"
    // http://127.0.0.1/login.php?username=%E5%BC%A0%E4%B8%89&password=zhang
    add = [add stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:add];
//    请求
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:1 timeoutInterval:2.0F];
//    连接
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//        反序列化
        id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
        NSLog(@"%@",result);
    }];
}

@end

post:

/**
 *  post
 */
- (void)postLogin{
    
    /**
     *  POST
     只是一个单纯的资源路径,是没有任何参数的,因为参数都在数据体里边。
     
     request 
        get的时候---什么都不需要做,因为系统默认的http方法(HTTPMethod)就是一个get
        post的时候---
                    1.需要指定HTTPMethod 是POST
                    2.指定了数据体的二进制。
                    POST出现中文是不需要转译的,因为系统帮助实现了。

       ****POST的参数和GET在URL里的参数格式是一样的,只是没有了 ?  &


        Connection 都是一样的

*/
    NSString *userName = @"zhangsan";
    NSString *userPwd = @"zhang";
    // 1.地址
    NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/login.php"];
    
    // 请求  post的数据体包含在这个里边
    // 2.可变的请求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:1 timeoutInterval:2.0F];
    // 2.1 制定的http的访问方法,服务器端才知道是以何种方式访问的
    request.HTTPMethod = @"POST";
    // 2.2 数据体的内容,
    NSString *bobyStr = [NSString stringWithFormat:@"username=%@&password=%@",userName,userPwd];
    
    // 跟服务器的交互全部传输的是二进制
    request.HTTPBody = [bobyStr dataUsingEncoding:NSUTF8StringEncoding];
    // 3.连接
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        //        反序列化
        id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
        NSLog(@"%@",result);
    }];

}
原文地址:https://www.cnblogs.com/pengpengzhang/p/4860360.html