iOS开发网络篇—GET请求和POST请求 分类: ios开发 2015-01-03 17:10 172人阅读 评论(0) 收藏
2015-01-03 17:10
761 查看
一、[b]GET请求和POST请求简单说明[/b]
创建GET请求
服务器:
创建POST请求
服务器:
二、比较
建议:提交用户的隐私数据一定要使用POST请求
相对POST请求而言,GET请求的所有参数都直接暴露在URL中,请求的URL一般会记录在服务器的访问日志中,而服务器的访问日志是黑客攻击的重点对象之一
用户的隐私数据如登录密码,银行账号等。
三、使用
1.通过请求头告诉服务器,客户端的类型(可以通过修改,欺骗服务器)
服务器:
2.加强对中文的处理
问题:URL不允许写中文
在GET请求中,相关代码段打断点以验证。
在字符串的拼接参数中,用户名使用“文顶顶”.
转换成URL之后整个变成了空值。
提示:URL里面不能包含中文。
解决:进行转码
调试查看:
服务器:
创建GET请求
1 // 1.设置请求路径 2 NSString *urlStr=[NSString stringWithFormat:@"http://192.168.1.53:8080/MJServer/login?username=%@&pwd=%@",self.username.text,self.pwd.text]; 3 NSURL *url=[NSURL URLWithString:urlStr]; 4 5 // 2.创建请求对象 6 NSURLRequest *request=[NSURLRequest requestWithURL:url]; 7 8 // 3.发送请求
服务器:
创建POST请求
1 // 1.设置请求路径 2 NSURL *URL=[NSURL URLWithString:@"http://192.168.1.53:8080/MJServer/login"];//不需要传递参数 3 4 // 2.创建请求对象 5 NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:URL];//默认为get请求 6 request.timeoutInterval=5.0;//设置请求超时为5秒 7 request.HTTPMethod=@"POST";//设置请求方法 8 9 //设置请求体 10 NSString *param=[NSString stringWithFormat:@"username=%@&pwd=%@",self.username.text,self.pwd.text]; 11 //把拼接后的字符串转换为data,设置请求体 12 request.HTTPBody=[param dataUsingEncoding:NSUTF8StringEncoding]; 13 14 // 3.发送请求
服务器:
二、比较
建议:提交用户的隐私数据一定要使用POST请求
相对POST请求而言,GET请求的所有参数都直接暴露在URL中,请求的URL一般会记录在服务器的访问日志中,而服务器的访问日志是黑客攻击的重点对象之一
用户的隐私数据如登录密码,银行账号等。
三、使用
1.通过请求头告诉服务器,客户端的类型(可以通过修改,欺骗服务器)
1 // 1.设置请求路径 2 NSURL *URL=[NSURL URLWithString:@"http://192.168.1.53:8080/MJServer/login"];//不需要传递参数 3 4 // 2.创建请求对象 5 NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:URL];//默认为get请求 6 request.timeoutInterval=5.0;//设置请求超时为5秒 7 request.HTTPMethod=@"POST";//设置请求方法 8 9 //设置请求体 10 NSString *param=[NSString stringWithFormat:@"username=%@&pwd=%@",self.username.text,self.pwd.text]; 11 //把拼接后的字符串转换为data,设置请求体 12 request.HTTPBody=[param dataUsingEncoding:NSUTF8StringEncoding]; 13 14 //客户端类型,只能写英文 15 [request setValue:@"ios+android" forHTTPHeaderField:@"User-Agent"];
服务器:
2.加强对中文的处理
问题:URL不允许写中文
在GET请求中,相关代码段打断点以验证。
在字符串的拼接参数中,用户名使用“文顶顶”.
转换成URL之后整个变成了空值。
提示:URL里面不能包含中文。
解决:进行转码
1 // 1.设置请求路径 2 NSString *urlStr=[NSString stringWithFormat:@"http://192.168.1.53:8080/MJServer/login?username=%@&pwd=%@",self.username.text,self.pwd.text]; 3 //转码 4 urlStr= [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 5 NSURL *url=[NSURL URLWithString:urlStr]; 6 7 // 2.创建请求对象 8 NSURLRequest *request=[NSURLRequest requestWithURL:url];
调试查看:
服务器:
相关文章推荐
- ios 同步Get请求的实现 分类: ios开发 2015-01-30 17:36 362人阅读 评论(0) 收藏
- iOS开发网络篇—网络编程基础 分类: ios开发 2015-01-21 19:24 140人阅读 评论(0) 收藏
- iOS开发网络篇—使用ASI框架进行文件下载 分类: ios开发 2015-02-02 10:43 133人阅读 评论(0) 收藏
- iOS开发网络篇—文件下载(一·不合理) 分类: ios开发 2015-01-24 16:02 244人阅读 评论(0) 收藏
- iOS开发网络篇—HTTP协议 分类: ios开发 2015-01-21 19:45 147人阅读 评论(0) 收藏
- iOS开发网络篇—搭建本地服务器 分类: ios开发 2015-01-21 19:05 257人阅读 评论(1) 收藏
- iOS开发网络篇—JSON介绍 分类: ios开发 2015-01-24 15:01 137人阅读 评论(0) 收藏
- 多线程之GCD(一) 分类: ios开发 2015-04-17 17:58 357人阅读 评论(0) 收藏
- iOS网络编程实践--蓝牙对等网络通信实例讲解 分类: ios开发 2015-01-01 13:40 160人阅读 评论(0) 收藏
- 【iOS】UIViewController、UINavigationController与UITabBarController的整合使用 分类: ios开发 2015-01-29 11:06 176人阅读 评论(0) 收藏
- Android开发之WebService介绍ZZ 分类: Android开发 2014-05-30 10:58 77人阅读 评论(0) 收藏
- 3/17/10 9:55:59 AM CST: [INFO] User settings file does not exist C:/Documents and Settings/Administrator/.m2/settings.xml 分类: 开发常见问题解决方案 2010-03-17 10:20 3448人阅读 评论(4) 收藏
- iOS开发之全局变量 分类: ios开发 2015-02-02 17:16 398人阅读 评论(0) 收藏
- ios UIScrollView 循环滑动 分类: ios开发 2015-01-29 17:22 138人阅读 评论(0) 收藏
- 调度器(scheduler) 分类: ios开发 Cocos2d-x 2015-02-05 13:58 199人阅读 评论(0) 收藏
- 如何去掉NavigationBar的backButton 分类: ios开发 2015-01-26 11:39 323人阅读 评论(0) 收藏
- iOS开发网络篇—GET请求和POST请求
- iOS 应用取消时间栏 分类: ios开发 2015-03-27 19:52 183人阅读 评论(0) 收藏
- ClippingNode的使用 分类: ios开发 Cocos2d-x 2015-02-09 18:17 112人阅读 评论(0) 收藏
- iOS开发网络篇—GET请求和POST请求