一、Post相关内容
1、有同步、异步的方式;
2、get(明文)浏览器里看,post(密文)把参数变成NSData
3、post可变字符串需要两个:
一个拼接URL+interface
req的时候只把URL+interface的字符串放进来
一个拼接key和value
然后把key和value的字符串变成data
然后单独设置data(key和value)
要强调一下这次请求的方式是POST
二、Post同步请求(以登录界面为例)
2.1、LogInViewController.m按钮方法(其他的都和Get一样)
-(void)btnDown
{
//酱油们
UITextField *count = (UITextField*)[self.view viewWithTag:5000];
UITextField *pass = (UITextField*)[self.view viewWithTag:5001];
[count resignFirstResponder];
[pass resignFirstResponder];
//请求们
NSDictionary *dic = [PostSyn postSynWithURL:@"http://10.0.8.8/sns" andInterface:@"/my/login.php" andKeyArr:@[@"username",@"password"] andValueArr:@[count.text,pass.text] andTarget:self];
if([dic respondsToSelector:@selector(stringWithFormat:)])
{//error了,直接退出
return;
}
if([dic[@"code"] isEqualToString:@"login_success"])
{
//push
MainViewController *main = [[MainViewController alloc] init];
[self.navigationController pushViewController:main animated:YES];
[main release];
}
else
{
//警报
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警报" message:dic[@"message"] delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
}
2.2、PostSyn.h——Post同步请求封装类
#import <Foundation/Foundation.h>
@interface PostSyn : NSObject
+(id)postSynWithURL:(NSString*)url andInterface:(NSString*)interface andKeyArr:(NSArray*)keyArr andValueArr:(NSArray*)valueArr andTarget:(id)targetVC;
@end
2.3、PostSyn.m——Post同步请求封装类
#import "PostSyn.h"
#import <UIKit/UIKit.h>
@implementation PostSyn
+(id)postSynWithURL:(NSString *)url andInterface:(NSString *)interface andKeyArr:(NSArray *)keyArr andValueArr:(NSArray *)valueArr andTarget:(id)targetVC
{
//4、准备两个可变字符串,一个拼地址、接口,一个拼key和value
NSMutableString *urlStr = [NSMutableString stringWithCapacity:0];//拼地址和接口
NSMutableString *contentStr = [NSMutableString stringWithCapacity:0];//拼key和value
//5、拼地址、接口
[urlStr appendFormat:@"%@%@",url,interface];
//6、拼key和value
for(int i = 0;i<keyArr.count;i++)
{
[contentStr appendFormat:@"%@=%@",keyArr[i],valueArr[i]];
if(i<keyArr.count-1)
{
[contentStr appendString:@"&"];
}
}
//7、准备请求体
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10];
//到目前为止请求体里有地址和接口,但是没有参数
//8、设置请求体的内容,里面的小括号把字符串转成了data当参数
[req setHTTPBody:[contentStr dataUsingEncoding:NSUTF8StringEncoding]];
//9、设置请求方式
[req setHTTPMethod:@"POST"];
NSError *error = nil;
//10、开始同步请求
NSData *backData = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:&error];
if(error)
{
NSLog(@"%@",error);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警报" message:@"error了" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
return @"error";
}
else
{
id dic = [NSJSONSerialization JSONObjectWithData:backData options:NSJSONReadingMutableLeaves error:&error];
if(error)
{
NSLog(@"%@",error);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警报" message:@"解析出错了" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
return @"error1";
}
else
{
return dic;
}
}
}
@end
三、Post异步请求(以登录界面为例)
3.1、LogInViewController.m四个协议方法(其他的都和Get一样)
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
MBProgressHUD *mb = (MBProgressHUD*)[self.view viewWithTag:12345];
[mb removeFromSuperview];
mb = nil;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警报" message:@"您的网络故障,不是我的事儿" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
[alert show];
[alert release];
NSLog(@"%@",error);
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//大小
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.data appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
MBProgressHUD *mb = (MBProgressHUD*)[self.view viewWithTag:12345];
[mb removeFromSuperview];
mb = nil;
NSError *error = nil;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:self.data options:NSJSONReadingMutableLeaves error:&error];
if(error)
{
NSLog(@"%@",error);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警报" message:@"解析有误" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
else
{
if([dic[@"code"] isEqualToString:@"login_success"])
{
MainViewController *main = [[MainViewController alloc] init];
[self.navigationController pushViewController:main animated:YES];
[main release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警报" message:dic[@"message"] delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
}
}
3.2、PostAsyn.h——Post异步请求封装类
#import <Foundation/Foundation.h>
@interface PostAsyn : NSObject
+(void)postAsynWithURL:(NSString*)url andInterface:(NSString*)interface andKeyArr:(NSArray*)keyArr andValueArr:(NSArray*)valueArr andDelegateTarget:(id)targetVc;
@end
3.3、PostAsyn.m——Post异步请求封装类
#import "PostAsyn.h"
@implementation PostAsyn
+(void)postAsynWithURL:(NSString *)url andInterface:(NSString *)interface andKeyArr:(NSArray *)keyArr andValueArr:(NSArray *)valueArr andDelegateTarget:(id)targetVc
{
//4、两个可变字符串
NSMutableString *urlStr = [NSMutableString stringWithCapacity:0];
NSMutableString *contentStr = [NSMutableString stringWithCapacity:0];
//5、拼地址接口
[urlStr appendFormat:@"%@%@",url,interface];
//6、拼key和value
for(int i = 0;i<keyArr.count;i++)
{
[contentStr appendFormat:@"%@=%@",keyArr[i],valueArr[i]];
if(i < keyArr.count-1)
{
[contentStr appendString:@"&"];
}
}
//7、准备请求体
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10];
[req setHTTPBody:[contentStr dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPMethod:@"POST"];
//8、准备connection
NSURLConnection *con = [NSURLConnection connectionWithRequest:req delegate:targetVc];
[con start];
}
@end