iOS之HTTP网络编程

来源:互联网 发布:数据体现不出邓肯作用 编辑:程序博客网 时间:2024/06/13 12:44

iOS之同步请求、异步请求、GET请求、POST请求

1、同步请求可以从因特网请求数据,一旦发送同步请求,程序将停止用户交互,知道服务器返回数据完成,才可以进行下一步操作;
2、异步请求不会阻塞主线程,而会建立一个新的线程来操作,用户发出异步请求后,依旧可以对UI进行操作,程序可以继续进行;
3、GET请求,将参数直接写在访问路径上。操作简单,不过容易被外界看到,安全性不高,地址最多255字节;
4、POST请求,将餐宿放在body里。POST请求操作相对复杂,需要将参数和地址分开,不过安全性高,参数放在body里,不易被捕获。

GET请求:

使用NSURLConnection创建异步下载请求。此时需要实现NSURLConnectionDelegate协议方法

#define GET_URL @"http://192.168.2.2/service/downloadnote?user_name="- (void)downloadData:(id)sender{    NSString* userNmae = @"wanghang";    //下载数据的URL    NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",GET_URL,username]];    //创建请求对象    NSURLRequest* request = [NSURLRequest requestWithURL:url];    //创建URLConnection对象,并设置当前类的对象为代理    NSURLConnection* connection = [NSURLConnection alloc] initWithRequest:request delegate:self];}

以下为常用的NSURLConnectionDelegate协议方法

- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error{    //此方法是请求链接失败时回调} - (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response{    //此方法是请求得到响应时调用}- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data{    //此方法是接收到数据后调用,一般在此对数据进行处理,例如解析}- (void)connectionDidFinishLoading:(NSURLConnection*)connection{    //此方法是请求完成后调用,一般可以在此刷新UI}

POST请求:

使用NSURLConnection创建异步上传请求。此时需要实现NSURLConnectionDelegate协议方法

#define POST_URL @"http://192.168.2.2/service/uploadnote?user_name="- (IBAction)uploadData:(id)sender{    NSString* userName = @"wanghang";    //需上传的内容    NSString* content = @"这里是需要上传的数据";    NSData* postData = [content dataUsingEncoding:NSUTF8StringEncoding];    NSURL* URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",POST_URL,userName]];    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];    [request setHTTPBody:postData];//设置请求体,即要上传的内容    [request setHTTPMethod:@"POST"];//设置请求方法为POST    [request setValue:[NSString stringWithFormat:@"%d",postData.length] forHTTPHeaderField:@"Content-Length"];    NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];}

NSURLConnection的send方法实现同步和异步下载请求

#define URL @"192.168.2.1"//使用NSURLConnection的sendAsynchronousRequest方法创建异步下载请求NSURL* url = [[NSURL alloc] initWithString:URL];    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];    NSOperationQueue* queue = [[NSOperationQueue alloc] init];    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse* response,NSData* data,NSError* connectionError){        if (!connectionError) {            NSString* str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];            NSLog(@"%@",str);        }    }];//使用NSURLConnection的sendSynchronousRequest方法创建同步下载请求    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];//该方法有数据类型为 nullable NSData * 的返回值    NSString* str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];    NSLog(@"%@",str);

NSURLConnection的send方法实现同步和异步上传请求

//使用NSURLConnection的sendAsynchronousRequest方法创建异步上传请求    NSOperationQueue* queue = [NSOperationQueue mainQueue];    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {        if (!connectionError) {            NSString* str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];        }    }];//使用NSURLConnection的sendSynchronousRequest方法创建同步上传请求    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];    NSString* str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

总结一下:

1、实现网络数据的上传和下载在广义上有两种方式:同步和异步
2、同步上传或下载使用的是NSURLConnection的sendSynchronousRequest方法
3、实现异步上传和下载有两种方式:
a.使用delegate方法[[NSURLConnection alloc] initWithRequest:request delegate:self]; 回调NSURLConnectionDelegate的几个方法
b.使用block块请求后的返回值直接在block块的参数中—sendAsynchronousRequest

0 0
原创粉丝点击