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

2017-03-16  本文已影响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请求。

上一篇下一篇

猜你喜欢

热点阅读