IOS 初学笔记 之 数据请求 POST
//第一步,创建URL NSURL *urls = [NSURLURLWithString:url];url 为请求链接// //第二步,创建请求NSMutableURLRequest*request= [[NSMutableURLRequestalloc]initWithURL:urls cachePolicy:NSURLRequestUseProtocolCachePolicytime
//第一步,创建URL
NSURL *urls = [NSURLURLWithString:url];
url 为请求链接
// //第二步,创建请求
NSMutableURLRequest*request= [[NSMutableURLRequestalloc]initWithURL:urls cachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:10];
[request setHTTPMethod:@"POST"];//设置请求方式为POST,默认为GET
NSString *str = [NSStringstringWithFormat:@"usere=%@&pwd=%@",zhStr,mmStr];//设置参数
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
// //第三步,连接服务器
NSData *received = [NSURLConnectionsendSynchronousRequest:request returningResponse:nilerror:nil];
//转换格式NSString//
NSString *str1 = [[NSStringalloc]initWithData:received encoding:NSUTF8StringEncoding];
// 转换格式NSDictionary
NSDictionary*jsonDict=[NSJSONSerializationJSONObjectWithData:receivedoptions:NSJSONReadingMutableLeaveserror:nil];
更多推荐
所有评论(0)