iOS 中GET 和 POST 请求

主要三步

1.url

2.request

3.connection


get请求


//    1.URL

    NSString *urlStr = [NSString stringWithFormat:@"http://www.baidu.com/?userName=%@&password=%@",self.userName.text,self.userPwd.text];

    NSURL *url = [NSURL URLWithString:urlStr];

//    2. request

    NSURLRequest *request = [NSURLRequest   requestWithURL:url];

//    3. connection

//    1>登陆完成之前 ,不能做后续工作,

//    2> 登陆过程中 ,可以允许用户干点别的会更好

//    3>让登陆在其他现程中进行。就不会阻塞主线程的工作

//    4> 结论:登陆也是异步访问,中间需要阻塞住

//    在并行队列中 用同步方法, 阻塞后续的执行!

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

        

        if (connectionError == nil) {

//        网络请求结束之后执行

//        将Data转换为字符串

        NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

        NSLog(@"%@  ,,, %@",str, [NSThread currentThread]);

//            更新界面

            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                self.loginResult.text = @"登陆完成";

            }];

        }

    }];

    

    NSLog(@"come here %@", [NSThread currentThread]);

    

}就这么简单

post 请求和GET请求区别

多了

注:添加东西要用 NSMutableURLRequest   添加数据用它  不添加用NSURLRequest  ,不然request.HTTPMethod = @"POST";// 默认是GET 请求会报错

//    Post?

    request.HTTPMethod = @"POST";// 默认是GET 请求

//    数据体?

    NSString *str = [NSString stringWithFormat:@"username=%@&password=%@", self.userName.text, self.userPwd.text];

    request.HTTPBody = [str dataUsingEncoding:NSUTF8StringEncoding];


总结:GET & POST

1.GET

1>URL

2>request

3>connection

2.POST

1>URL

2>request

多了

1. request.HTTPMethod = @"POST";// 默认是GET 请求

2. NSString *str = [NSString stringWithFormat:@"username=%@&password=%@", self.userName.text, self.userPwd.text];

3. request.HTTPBody = [str dataUsingEncoding:NSUTF8StringEncoding];


3>connection

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值