Web Service所支持的HTTP请求方法包括POST、GET、PUT、DELETE等,十分丰富,我这里主要记录一下使用GET方法进行同步、异步请求以及使用POST进行异步请求的主要过程。
一、使用GET进行同步请求
使用GET进行的同步请求比较简单,具体设计到的代码如下:
- (void)startRequest{
//Web Service 的URL
NSString* strURL = [[NSString alloc] initWithFormat:@"http://www.51work6.com/service/mynotes/WebService.php?email=%@&type=%@&action=%@",@"注册的邮箱地址",@"JSON",@"query" ];
//用UTF-8进行编码
strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//将字符串转换为NSURL类型
NSURL* url = [NSURL URLWithString:strURL];
//实例化request请求对象
NSURLRequest* request = [[NSURLRequest alloc] initWithURL:url];
//发送同步请求,同时阻塞线程,等待Web Service应答
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//收到Web Service应答
NSDictionary* resDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
[self reloadView:resDict];
}
使用GET方法进行请求,请求的参数会全部暴露在URL后面,接触到的人都可以看到,这是不安全的请求方式,同时,同步请求方式,在程序发动请求时,会将线程堵塞住,直到Web Service返回应答,这种方式的请求用户体验不好。
二、使用GET进行异步请求
我们会使用NSURLConnection委托协议NSURLConnectionDataDelegate来进行异步请求,在请求的不同阶段会回调下面几个方法:
connection:didReceiveData: 连接成功建立后,收到服务器端发来的data会回调这个方法,数据量多时会多次调用这个方法。
connection:didiFailWithError: 加载数据过程中出现异常时回调的方法。
connectionDidFinishLoading: 成功加载全部的数据后调用的方法。
具体使用时用到的主要代码如下:
#pragma mark - NSURLConnection回调方法
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
//不断接受数据并加载到data中
[self.data appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"%@",[error localizedDescription]);
}
//数据接受完成后调用
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
//解析json数据
NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:self.data options:NSJSONReadingMutableContainers error:nil];
[self reloadView:dict];
}
- (void)startRequest{
//向51work6服务器发送请求的url
NSString* strURL = [[NSString alloc] initWithFormat:@"http://www.51work6.com/service/mynotes/WebService.php?email=%@&type=%@&action=%@",@"注册邮箱",@"JSON",@"query" ];
//转化为UTF-8编码
strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:strURL];
NSURLRequest* request = [[NSURLRequest alloc] initWithURL:url];
//建立connection连接,建立完成后就回发送请求,然后不断接受数据
NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
//实例化data
self.data = [NSMutableData new];
}
}
使用异步的方式来进行Web Service请求,不会阻塞线程,能明显优化用户的体验,可是对于安全性的问题,就需要使用POST请求了。
三、使用POST进行异步请求
使用POST进行异步请求,能比较好的保证安全性的问题,其实,这个过程与使用GET的异步请求十分相似,只是使用NSMutableURLRequest类代替了NSURLRequest类。具体使用的主要代码如下:
- (void)startRequest{
//向51work6服务器发送请求的url
NSString* strURL = @"http://www.51work6.com/service/mynotes/WebService.php";
//转化为UTF-8编码
strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:strURL];
//设置Web Service请求的参数
NSString* post = [NSString stringWithFormat:@"email=%@&type=%@&action=%@",@"注册邮箱",@"JSON",@"query"];
//将参数转换为NSData类型
NSData* postData = [post dataUsingEncoding:NSUTF8StringEncoding];
//发送异步请求
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPBody:postData];
[request setHTTPMethod:@"POST"];
//建立connection连接,建立完成后就回发送请求,然后不断接受数据
NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
//实例化data
self.data = [NSMutableData new];
}
}