网络篇----GET、POSt请求
一、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一般会记录在服务器的访问日志中,而服务器的访问日志是黑客攻击的重点对象之一
用户的隐私数据如登录密码,银行账号等。
三、使用
1.通过请求头告诉服务器,客户端的类型(可以通过修改,欺骗服务器)
// 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]; //客户端类型,只能写英文 [request setValue:@"ios+android" forHTTPHeaderField:@"User-Agent"];
服务器:
2.加强对中文的处理
问题:URL不允许写中文
在GET请求中,相关代码段打断点以验证。
在字符串的拼接参数中,用户名使用“文顶顶”.
转换成URL之后整个变成了空值。
提示:URL里面不能包含中文。
解决:进行转码
// 1.设置请求路径 NSString *urlStr=[NSString stringWithFormat:@"http://192.168.1.53:8080/MJServer/login?username=%@&pwd=%@",self.username.text,self.pwd.text]; //转码 urlStr= [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url=[NSURL URLWithString:urlStr]; // 2.创建请求对象 NSURLRequest *request=[NSURLRequest requestWithURL:url];
调试查看:
服务器:
二、发送json给服务器
超时属性
//5秒后请求超时,默认是60秒
requsret.timeoutInterval=5;
上面的例子是发送两个普通的字符串发给服务器,
为什么发送json给服务器:是由服务器规定的
// 1.URL NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/order"]; // 2.请求 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 3.请求方法 request.HTTPMethod = @"POST"; // 4.设置请求体(请求参数) // 创建一个描述订单信息的JSON数据 NSDictionary *orderInfo = @{ @"shop_id" : @"1243324", @"shop_name" : @"啊哈哈哈", @"user_id" : @"899" }; NSData *json = [NSJSONSerialization dataWithJSONObject:orderInfo options:NSJSONWritingPrettyPrinted error:nil]; request.HTTPBody = json; // 5.设置请求头:这次请求体的数据不再是普通的参数,而是一个JSON数据 如果是普通数据就不需要设置请求头 [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; // 6.发送请求 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { if (data == nil || connectionError) return; NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; NSString *error = dict[@"error"]; if (error) { [MBProgressHUD showError:error]; } else { NSString *success = dict[@"success"]; [MBProgressHUD showSuccess:success]; } }];
三、多值参数
一个参数名,可能会对应多个参数值
// 1.URL NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/weather"]; // 2.请求 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 3.请求方法 request.HTTPMethod = @"POST"; // 4.设置请求体(请求参数)多值传入的话需要分开写 NSMutableString *param = [NSMutableString string]; [param appendString:@"place=beijing"]; [param appendString:@"&place=tianjin"]; [param appendString:@"&place=meizhou"]; request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding]; // 5.发送请求 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { if (data == nil || connectionError) return; NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; NSString *error = dict[@"error"]; if (error) { [MBProgressHUD showError:error]; } else { // NSArray *weathers = dict[@"weathers"]; NSLog(@"%@", dict); } }];