//同步POST
-(void)synPost{
//获取URL接口,不含参数
NSString *str = @"http://www.haninfo.cc:2060/Login/LoginData.asmx/Login";
//转码---拼接方法,为避免参数是汉字时,打印结果不显示汉字
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:str];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:10];
//post方式的参数封装
NSString *dataStr = [NSString stringWithFormat:@"sLogin=%@&sVerifyCode=%@&sPadId=%@",@"yyj",@"",@""];
//将参数列表转换成data
NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
//设置参数体
[request setHTTPBody:data];
//设置请求方式
[request setHTTPMethod:@"POST"];
//发送请求
NSData *responData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//解析
id obj = [NSJSONSerialization JSONObjectWithData:responData options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",obj);
//获取URL接口,不含参数
NSString *str = @"http://www.haninfo.cc:2060/Login/LoginData.asmx/Login";
//转码---拼接方法,为避免参数是汉字时,打印结果不显示汉字
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:str];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:10];
//post方式的参数封装
NSString *dataStr = [NSString stringWithFormat:@"sLogin=%@&sVerifyCode=%@&sPadId=%@",@"yyj",@"",@""];
//将参数列表转换成data
NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
//设置参数体
[request setHTTPBody:data];
//设置请求方式
[request setHTTPMethod:@"POST"];
//发送请求
NSData *responData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//解析
id obj = [NSJSONSerialization JSONObjectWithData:responData options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",obj);
}
=====================================================
//异步POST
-(void)asynPOST{
NSString *str = @"http://www.haninfo.cc:2060/Login/LoginData.asmx/Login";
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//把汉字转换成字符串
NSURL *url = [NSURL URLWithString:str];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:10];
//post方式的参数封装
NSString *dataStr = [NSString stringWithFormat:@"sLogin=%@&sVerifyCode=%@&sPadId=%@",@"yyj",@"",@""];
//将参数列表转换成data
NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
//设置参数体
[request setHTTPBody:data];
//设置请求方式
[request setHTTPMethod:@"POST"];
NSString *str = @"http://www.haninfo.cc:2060/Login/LoginData.asmx/Login";
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//把汉字转换成字符串
NSURL *url = [NSURL URLWithString:str];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:10];
//post方式的参数封装
NSString *dataStr = [NSString stringWithFormat:@"sLogin=%@&sVerifyCode=%@&sPadId=%@",@"yyj",@"",@""];
//将参数列表转换成data
NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
//设置参数体
[request setHTTPBody:data];
//设置请求方式
[request setHTTPMethod:@"POST"];
//方法一:系统封装好的(不在需要代理方法)
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dic);
}];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dic);
}];
//方法二:需要代理方法
[NSURLConnection connectionWithRequest:request delegate:self];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
_muData = [NSMutableData data];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[_muData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
//解析
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
//连接服务器失败
NSLog(@"错误信息:%@",error);
}
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
_muData = [NSMutableData data];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[_muData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
//解析
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
//连接服务器失败
NSLog(@"错误信息:%@",error);
}