IOS http网络请求

来源:互联网 发布:苹果wcdma是什么网络 编辑:程序博客网 时间:2024/05/29 14:04

关于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 = [AFHTTPClientclientWithBaseURL:[NSURLURLWithString:kBaseURL]];

    

    NSURLRequest *post = [clientrequestWithMethod:methodpath:path parameters:@{ }];

    

//     2.创建AFJSONRequestOperation对象

        NSOperation *op = [AFJSONRequestOperationJSONRequestOperationWithRequest: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];

    }










原创粉丝点击