#pragma mark-------------------------------GET同步-------------
- (IBAction)getT:(UIButton *)sender
{
// NSString 和 NSURL(请求地址)
// NSMutableURLRequest (在这个地址是 GET 还是 POST 请求)
// NSURLConnection (链接方式, 同步/异步)
NSString *urlStr = @"http://m1.music.126.net/gpi8Adr_-pfCuP7ZXk_F2w==/2926899953898363.mp3";
NSURL *url = [NSURL URLWithString:urlStr];
// 设置链接请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 设置请求方式, 默认是 GET
[request setHTTPMethod:@"GET"];
// NSURLConnection 向携带了地址的那个 request 发送了一个同步请求, 返回一个 NSData
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// JSON解析
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dic);
self.view.backgroundColor = [UIColor redColor];
}
#pragma mark--------------------GET异步------------
- (IBAction)getY:(UIButton *)sender
{
NSString *urlStr = @"http://m1.music.126.net/gpi8Adr_-pfCuP7ZXk_F2w==/2926899953898363.mp3";
NSURL *url = [NSURL URLWithString:urlStr];
// 设置链接请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 设置请求方式, 默认是 GET
[request setHTTPMethod:@"GET"];
// 根据 request 发送请求
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
[connection start];
}
// 代理方法
// 开始发送请求
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// 放在这里初始化, 确保的是我的每次网络请求 data 都是空的
self.data = [NSMutableData data];
}
// 正在接受数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// 防止一段段的数据出现, 所以直接拼接到后面
[self.data appendData:data];
}
// 结束网络请求
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"结束网络请求");
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:self.data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"dic====%@",dic);
}
// 抛出异常方法
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
}
#pragma mark--------------------------POST异步--------------------------
- (IBAction)postY:(UIButton *)sender
{
NSString *urlStr = @"http://172.21.45.27/index.php";
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *parmaStr = @"userName=mm&password=123";
NSData *parData = [parmaStr dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:parData];
// block 回调形式的异步网络请求
//[NSOperationQueue mainQueue] 回到主线程
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dic);
}];
}
#pragma mark------------POST同步--------
- (IBAction)postT:(UIButton *)sender
{
// POST 和 GET
NSString *urlStr = @"http://172.21.45.27/index.php";
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 转码, NSString 转化成 NSData 和 NSData 转化为 NSString
NSString *parmaStr = @"userName=mm&password=123";
// post请求的 body 一定要通过 UTF-8进行转码
NSData *parData = [parmaStr dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:@"POST"];
// body 实际就是在使用中 POST 和 GET 最大的区别, get 不需要携带 body, 只有 Post 需要
[request setHTTPBody:parData];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dic);
}
- (IBAction)getT:(UIButton *)sender
{
// NSString 和 NSURL(请求地址)
// NSMutableURLRequest (在这个地址是 GET 还是 POST 请求)
// NSURLConnection (链接方式, 同步/异步)
NSString *urlStr = @"http://m1.music.126.net/gpi8Adr_-pfCuP7ZXk_F2w==/2926899953898363.mp3";
NSURL *url = [NSURL URLWithString:urlStr];
// 设置链接请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 设置请求方式, 默认是 GET
[request setHTTPMethod:@"GET"];
// NSURLConnection 向携带了地址的那个 request 发送了一个同步请求, 返回一个 NSData
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// JSON解析
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dic);
self.view.backgroundColor = [UIColor redColor];
}
#pragma mark--------------------GET异步------------
- (IBAction)getY:(UIButton *)sender
{
NSString *urlStr = @"http://m1.music.126.net/gpi8Adr_-pfCuP7ZXk_F2w==/2926899953898363.mp3";
NSURL *url = [NSURL URLWithString:urlStr];
// 设置链接请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 设置请求方式, 默认是 GET
[request setHTTPMethod:@"GET"];
// 根据 request 发送请求
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
[connection start];
}
// 代理方法
// 开始发送请求
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// 放在这里初始化, 确保的是我的每次网络请求 data 都是空的
self.data = [NSMutableData data];
}
// 正在接受数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// 防止一段段的数据出现, 所以直接拼接到后面
[self.data appendData:data];
}
// 结束网络请求
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"结束网络请求");
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:self.data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"dic====%@",dic);
}
// 抛出异常方法
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
}
#pragma mark--------------------------POST异步--------------------------
- (IBAction)postY:(UIButton *)sender
{
NSString *urlStr = @"http://172.21.45.27/index.php";
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *parmaStr = @"userName=mm&password=123";
NSData *parData = [parmaStr dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:parData];
// block 回调形式的异步网络请求
//[NSOperationQueue mainQueue] 回到主线程
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dic);
}];
}
#pragma mark------------POST同步--------
- (IBAction)postT:(UIButton *)sender
{
// POST 和 GET
NSString *urlStr = @"http://172.21.45.27/index.php";
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 转码, NSString 转化成 NSData 和 NSData 转化为 NSString
NSString *parmaStr = @"userName=mm&password=123";
// post请求的 body 一定要通过 UTF-8进行转码
NSData *parData = [parmaStr dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:@"POST"];
// body 实际就是在使用中 POST 和 GET 最大的区别, get 不需要携带 body, 只有 Post 需要
[request setHTTPBody:parData];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dic);
}