IOS 请求服务器的两种方法GET 和 POST

来源:互联网 发布:Windows鼠标指针 编辑:程序博客网 时间:2024/05/18 22:40

接上篇  同步请求  和  异步请求

上篇链接http://blog.csdn.net/lc_obj/article/details/17604395

这次要总结的是请求服务器的两种方法GET 和  POST

GET 和 POST 的区别

1. get是从服务器上获取数据,post是向服务器传送数据。
2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到。post是通过HTTP post机制,将表单内各个字段与其内容放置在HTML HEADER内一起传送到ACTION属性所指的URL地址。用户看不到这个过程。
3. 对于get方式,服务器端用Request.QueryString获取变量的值,对于post方式,服务器端用Request.Form获取提交的数据。
4. get传送的数据量较小,不能大于2KB。post传送的数据量较大,一般被默认为不受限制。但理论上,IIS4中最大量为80KB,IIS5中为100KB。
5. get安全性非常低,post安全性较高。但是执行效率却比Post方法好。 


建议:
1、get方式的安全性较Post方式要差些,包含机密信息的话,建议用Post数据提交方式;
2、在做数据查询时,建议用Get方式;而在做数据添加、修改或删除时,建议用Post方式;


这里以一个小项目查询手机号归属地为例,后面会附上这个demo的
在这里首先要感谢 http://www.webxml.com.cn 这个网站,
这个网站提供了很多服务器端的接口,才能使我完成这个项目。
在这里我选的是手机号码归属地查询


话不多说下面看项目
结果效果截图,输入手机号然后点击查询

                   

首先有几个属性
@property (retain, nonatomic) NSMutableString *theResult;  //用于存储请求结果数据转换成的字符串
@property (retain, nonatomic) NSMutableData  *theResultData; //用于存储请求结果数据


@property (retain, nonatomic) IBOutlet UITextField *phoneNumber;  //用于输入手机号的文本框
@property (retain, nonatomic) IBOutlet UILabel *labelResult;  //显示结果的标签
- (IBAction)searchAttribytion:(id)sender;     //按钮响应方法




首先GET方法


这几行代码写在点击按钮所响应的方法


  //GET方式
     //组合需要往服务器传输的参数
    NSString *phoneNumber = self.phoneNumber.text;
    NSString *ID = @"";
    NSString *tempString = [NSString stringWithFormat:@"mobileCode=%@&userID=%@",phoneNumber,ID];
    
    //获取网址字符串
    NSString *httpString = [@"http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo" stringByAppendingFormat:tempString];
    //将字符串做成URL
    NSURL *URL = [NSURL URLWithString:httpString];
    
    //创建请求,最长时间60秒
    NSURLRequest *pRquest =[NSURLRequest requestWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    //连接服务器
    [NSURLConnection connectionWithRequest:pRquest delegate:self];
    
    
    
    //POST方式
    
    //这里与GET方式的第一个不同点是不用把需要参数直接封装到网址里
    NSString *pStr = [NSString stringWithFormat:@"http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo"];
    //将字符串合成URL
    NSURL *url = [NSURL URLWithString:pStr];
    
    //创建请求,这里是第二个不同点,创建请求的类不同。
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    
    //设置post请求的参数 ,
    NSString *phoneNumber = self.phoneNumber.text;
    NSString *ID = @"";
    NSString *tempString = [NSString stringWithFormat:@"mobileCode=%@&userID=%@",phoneNumber,ID];
    //把参数封装为data
    NSData *data = [tempString dataUsingEncoding:NSUTF8StringEncoding];
    
    //设置参数,第三个不同点,需要把参数封装到body体中,并且body体数据位data型
    [request setHTTPBody:data];
    //设置请求方式,注意这里必须设置请求方式为POST,否则默认为GET方式
    [request setHTTPMethod:@"POST"];
    
    //创建连接
    [NSURLConnection connectionWithRequest:request delegate:self];
    
  以上就是GET 与  POST 两个方式的对比区别,下面跟上篇方法差不多
    
    //服务器开始响应请求
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    //初始化两个变量
    self.theResult = [NSMutableString string];
    self.theResultData = [NSMutableData data];
}


//开始接受数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    //把数据存储
    [self.theResultData appendData:data];
}


//数据接受完毕
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //把返回的data型数据转化为NSString
    self.theResult = [[NSMutableString alloc]initWithData:self.theResultData encoding:NSUTF8StringEncoding];
    //打印从服务器返回的数据
    NSLog(@"result = %@",self.theResult);
    [self handleTheResult:nil];
}


//请求错误
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    //打印错误信息
    NSLog(@"%@",[error localizedDescription]);
}


//结果处理
- (void)handleTheResult:(id)sender
{
    //处理字符串,根据返回字符串
    //从第78位截取
    NSString *temp = [self.theResult substringFromIndex:78];
    //把最后几位截掉
    NSString *result = [temp substringWithRange:NSMakeRange(0, [temp length]-9)];
    NSLog(@"%@",result);
    //把结果显示在屏幕上
    self.labelResult.text = result;
}


好了,这就是全部了,下面附上这个demo

http://download.csdn.net/detail/u012884714/6774115


写给自己,若有错误欢迎指正——LC







    
    

0 0