IOS http网络请求

关于ios开发。常见的网络请求有GET请求, POS请求, 

给服务器传输数据的方式不同

1.GET是通过网址字符串,所传输的数据显示在网址里。

2.POST是通过data,数据被转换,无法直接读取,。

数据链接方式:

-同步连接:只有数据请求完成了之后才会执行后面的代码,只做一件事,容易出现页面的卡死。

-异步连接:在请求数据的同时不影响后面代码的执行。可以同时去做多件事,

get请求:

    //创建URL对象

    NSString *urlStr = [kAuthorizeURLstringByAppendingFormat:@"?display=mobile&client_id=%@&redirect_uri=%@",kAppKey, kRedirectURI];

    NSURL *url = [NSURLURLWithString:urlStr];

    //创建请求(封装一个请求,保存发给服务器的全部数据)

    NSURLRequest *request = [NSURLRequestrequestWithURL:url];

    //发送请求

    [NSURLConnectionsendAsynchronousRequest:requestqueue:[NSOperationQueuemainQueue] completionHandler:^(NSURLResponse *_Nullable response, NSData * _Nullable data,NSError * _Nullable connectionError) {

        if (!connectionError) {

            

        }else{

           

        }

    }];

post请求:
使用 AFNetworking进行网络请求

   

    // 1.创建post请求路径

    AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:kBaseURL]];

    

    NSURLRequest *post = [client requestWithMethod:method path:path parameters:@{ }];

    

//     2.创建AFJSONRequestOperation对象

        NSOperation *op = [AFJSONRequestOperation JSONRequestOperationWithRequest:post

        success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

            if (success == nil) return;

            success(JSON);

        }

        failure : ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {

            if (failure == nil) return;

    

            failure(error);

        }];   

//      3.发送请求

        [op start];

    }










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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值