经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » iOS » 查看文章
ios开发网络篇—Get请求和Post请求 - 转
来源:cnblogs  作者:久依  时间:2018/12/14 9:25:44  对本文有异议

简单说明:建议提交用户的隐私数据一定要使用Post请求 
相对Post请求而言,Get请求的所有参数都直接暴露在URL中,请求的URL一般会记录在服务器的访问日志中,而服务器的访问日志是黑客攻击的重点对象之一 
用户的隐私数据如登录密码,银行帐号等

示例代码

  1. #define CURRENT_SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
  2. #define CURRENT_SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height - 64)
  3. #define BUTTON_WIDTH 80
  4. #define BUTTON_HEIGHT 40
  5.  
  6. @interface ViewController ()
  7. //GET 请求
  8. @property(nonatomic,strong) UIButton *getButton;
  9. //POST 请求
  10. @property(nonatomic,strong) UIButton *postButton;
  11. @end
  12.  
  13. @implementation ViewController
  14. - (void)viewDidLoad {
  15. [super viewDidLoad];
  16. _getButton = [[UIButton alloc] initWithFrame:CGRectMake(CURRENT_SCREEN_WIDTH/2 - BUTTON_WIDTH/2,
  17. CURRENT_SCREEN_HEIGHT/2 - BUTTON_HEIGHT,
  18. BUTTON_WIDTH,
  19. BUTTON_HEIGHT)];
  20. [_getButton setTitle:@"GET 请求" forState:UIControlStateNormal];
  21. [_getButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
  22. [_getButton addTarget:self
  23. action:@selector(getClick)
  24. forControlEvents:UIControlEventTouchUpInside];
  25. [self.view addSubview:_getButton];
  26. _postButton = [[UIButton alloc] initWithFrame:CGRectMake(CURRENT_SCREEN_WIDTH/2 - BUTTON_WIDTH/2,
  27. _getButton.frame.origin.y + _getButton.frame.size.height + 60,
  28. BUTTON_WIDTH,
  29. BUTTON_HEIGHT)];
  30. [_postButton setTitle:@"POST 请求" forState:UIControlStateNormal];
  31. [_postButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
  32. [_postButton addTarget:self
  33. action:@selector(postClick)
  34. forControlEvents:UIControlEventTouchUpInside];
  35. [self.view addSubview:_postButton];
  36. }
  37. /* get 请求 */
  38. -(void)getClick{
  39. //请求 URL
  40. NSString* urlStr = [NSString stringWithFormat:@"https://m.che168.com/beijing/?pvareaid=%d",110100];
  41. //封装成 NSURL
  42. NSURL* url = [NSURL URLWithString:urlStr];
  43. //初始化 请求对象
  44. NSURLRequest* request = [[NSURLRequest alloc] initWithURL:url];
  45. //也可以这样初始化对象
  46. //NSURLRequest* request = [NSURLRequest requestWithURL:url];
  47. //发送请求 默认为 GET 请求
  48. //1 、获得会话对象
  49. NSURLSession *session = [NSURLSession sharedSession];
  50. // 2、第一个参数:请求对象
  51. // 第二个参数:completionHandler回调(请求完成【成功|失败】的回调)
  52. // data:响应体信息(期望的数据)
  53. // response:响应头信息,主要是对服务器端的描述
  54. // error:错误信息,如果请求失败,则error有值
  55. NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  56. if(!error){
  57. NSLog(@"请求加载成功。。。");
  58. //说明:(此处返回的数据是JSON格式的,因此使用NSJSONSerialization进行反序列化处理)
  59. // NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
  60. //如果是字符串则直接取出
  61. NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  62. NSLog(@"GET 请求返回的结果是:%@",[str substringToIndex: 300]);
  63. }
  64. }];
  65. //执行任务
  66. [dataTask resume];
  67. /* ------------ ios9 之前请求方法,之后改成 NSURLSession 请求 --------------
  68. [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
  69. if(!connectionError){
  70. NSLog(@"加载成功。。。");
  71. NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  72. NSLog(@"加载的内容是:%@",[str substringToIndex:200]);
  73. }else{
  74. NSLog(@"加载失败");
  75. }
  76. }];
  77. */
  78. }
  79. /* POST 请求 */
  80. -(void)postClick{
  81. NSString *urlStr = [NSString stringWithFormat:@"https://m.che168.com/"];
  82. //转码
  83. // stringByAddingPercentEscapesUsingEncoding 只对 `#%^{}[]|\"<> 加空格共14个字符编码,不包括”&?”等符号), ios9将淘汰
  84. // ios9 以后要换成 stringByAddingPercentEncodingWithAllowedCharacters 这个方法进行转码
  85. urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[[NSCharacterSet characterSetWithCharactersInString:@"?!@#$^&%*+,:;='\"`<>()[]{}/\\| "] invertedSet]];
  86. NSURL *url = [NSURL URLWithString:urlStr];
  87. //创建会话对象
  88. NSURLSession *session = [NSURLSession sharedSession];
  89. //创建请求对象
  90. NSMutableURLRequest *request =[[NSMutableURLRequest alloc] initWithURL:url];
  91. [request setHTTPMethod:@"POST"];
  92. [request setHTTPBody:[@"a=1&b=2&c=3&type=json" dataUsingEncoding:NSUTF8StringEncoding]];
  93. //根据会话对象创建一个 Task(发送请求)
  94. NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  95. if(!error){
  96. //8.解析数据
  97. // NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
  98. // NSLog(@"%@",dict);
  99. NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  100. NSLog(@"POST 加载的内容是:%@",[str substringToIndex:200]);
  101. }else{
  102. NSLog(@"请求发生错误:%@", [error description]);
  103. }
  104. }];
  105. [dataTask resume]; //执行任务
  106. }

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号