使用POST的方法请求:
//访问的服务器地址
NSString * URLString = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";
NSURL * URL = [NSURL URLWithString:URLString];
//设置
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
//设置网络请求方法为post
[request setHTTPMethod:@"post"];
//传入后台数据必须为NSData
NSData * postData = [@"date=20131129&startRecord=2&len=20&udid=1234567890&terminalType=Iphone&cid=213" dataUsingEncoding:NSUTF8StringEncoding];
//客户端向服务器提交数据,NSData类型,(客户端和服务器之间传输的数据,都是NSData类型)
<span style="color:#cc0000;">[request setHTTPBody:postData];</span>
//连接服务器,同步连接
NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"ST = %@",[response MIMEType]);
if (data !=nil) {
NSString * STR = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"STR = %@",STR);
}
总结:C/S
1.客户端发起网络请求
2.连接服务器,服务器处理客户端的请求
3.服务器向客户端返回数据