操作类
NSURL
NSURLRequest 与 NSURLResponse
NSURLConnection : 同步存取和异步存取
异步存取实例:
// 发送URL
NSString *url = [[NSString alloc] initWithFormat:@"http://www.***.com"];
// 发送参数
NSString *postMsg = [NSString stringWithFormat:@"uid=admin&pwd=123456"];
NSData *postData = [postMsg dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSMutableData *_data = [[NSMutableData alloc] initWithLength:0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
// 收到响应时, 会触发
- (void)connection:(NSURLConnection *)aConnection didReceiveResponse:(NSURLResponse *)aResponse;
// 你可以在里面判断返回结果, 或者处理返回的http头中的信息
// 数据接收
- (void)connection:(NSURLConnection *)aConn didReceiveData:(NSData *)data { [_data appendData:data]; }
// 网络错误时触发
- (void)connection:(NSURLConnection *)aConn didFailWithError:(NSError *)error;
// 全部数据接收完毕时触发
- (void)connectionDidFinishLoading:(NSURLConnection *)aConn;