zoukankan      html  css  js  c++  java
  • GET请求和POST请求

    一、创建GET请求

    //    1.设置请求路径
        NSString *urlStr=[NSString stringWithFormat:@"http://192.168.1.53:8080/MJServer/login?username=%@&pwd=%@",self.username.text,self.pwd.text];
        NSURL *url=[NSURL URLWithString:urlStr];
        
    //    2.创建请求对象
        NSURLRequest *request=[NSURLRequest requestWithURL:url];
        
    //    3.发送请求

    二、创建POST请求

    // 1.设置请求路径
        NSURL *URL=[NSURL URLWithString:@"http://192.168.1.53:8080/MJServer/login"];//不需要传递参数
        
    //    2.创建请求对象
        NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:URL];//默认为get请求
        request.timeoutInterval=5.0;//设置请求超时为5秒
        request.HTTPMethod=@"POST";//设置请求方法
        
        //设置请求体
        NSString *param=[NSString stringWithFormat:@"username=%@&pwd=%@",self.username.text,self.pwd.text];
        //把拼接后的字符串转换为data,设置请求体
        request.HTTPBody=[param dataUsingEncoding:NSUTF8StringEncoding];
        
    //    3.发送请求

    三、比较

    建议:提交用户的隐私数据一定要使用POST请求

    相对POST请求而言,GET请求的所有参数都直接暴露在URL中,请求的URL一般会记录在服务器的访问日志中,而服务器的访问日志是黑客攻击的重点对象之一

    注意:URL里面不能包含中文,在拼接参数中如果有中文存在,则转化后的URL为nil,所以需要对URL作如下处理:

    NSString *urlStr= @"张三'";
        NSURL *url = [NSURL URLWithString:urlStr];  // 此时的url为nil
        
        NSString *newURLStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
        NSURL *newURL = [NSURL URLWithString:newURLStr];  //此时的newURL有值
        
        NSURLRequest *request = [NSURLRequest requestWithURL:newURL];
    
    /*
    
    @interface NSCharacterSet (NSURLUtilities)
    + (NSCharacterSet *)URLUserAllowedCharacterSet NS_AVAILABLE(10_9, 7_0);
    + (NSCharacterSet *)URLPasswordAllowedCharacterSet NS_AVAILABLE(10_9, 7_0);
    + (NSCharacterSet *)URLHostAllowedCharacterSet NS_AVAILABLE(10_9, 7_0);
    + (NSCharacterSet *)URLPathAllowedCharacterSet NS_AVAILABLE(10_9, 7_0);
    + (NSCharacterSet *)URLQueryAllowedCharacterSet NS_AVAILABLE(10_9, 7_0);
    + (NSCharacterSet *)URLFragmentAllowedCharacterSet NS_AVAILABLE(10_9, 7_0);
    */

    四、数据加密 

    摘自文顶顶微博,具体见 http://www.cnblogs.com/wendingding/p/3813723.html 文顶顶微博

  • 相关阅读:
    ES6 一些新特性的总结
    前端模块化开发的规范:AMD与CDM
    webpack与grunt/glub 的比较
    前端总结(一)
    前端性能的优化
    Typescript 常见写法
    显示模式------行内元素、块元素,行内块元素
    浏览器前缀及内核
    BFC规范
    数据库习题练习
  • 原文地址:https://www.cnblogs.com/LyChen/p/5102835.html
Copyright © 2011-2022 走看看