美文网首页
iOS开发网络篇—GET请求和POST请求

iOS开发网络篇—GET请求和POST请求

作者: 奉行尹先生 | 来源:发表于2017-03-16 11:29 被阅读0次

    GET请求和POST请求简单说明

    创建GET请求:

    //    1.设置请求路径

    NSString *urlStr=[NSString stringWithFormat:@"http://192.168.1.53:8080/MJServer/login?username=%@&pwd=%@",self.username.text,self.pwd.text];

    //    2.创建请求对象

    NSURL *url=[NSURL URLWithString:urlStr];

     //    3.发送请求

    NSURLRequest *request=[NSURLRequest requestWithURL:url];

    创建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];

    总结:

    相对post请求而言,get请求是把所有参数都直接暴露在URL中,请求的URL一般会记录在服务器的访问日志中,而服务器的访问日志是黑客攻击的重点对象之一,所以秘密信息最好是用post请求。

    相关文章

      网友评论

          本文标题:iOS开发网络篇—GET请求和POST请求

          本文链接:https://www.haomeiwen.com/subject/xzwdnttx.html