/*git
HTTP 是應用層的網絡傳輸協議,對HTTP請求主要流行的方式有兩種 GET請求以及POST請求安全
GET請求和POST請求區別和聯繫:服務器
1.GET請求,服務器網址以及參數都會出如今咱們網絡請求的接口中,也就是說參數會做爲請求接口的一部分. 而POST請求在接口中只有服務器網址,而參數會做爲請求體交給服務器網絡
2.由於GET請求參數會做爲接口的一部分出如今接口中,因此信息容易捕獲,不安全.而POST請求參數會被封裝在請求體中,做爲二進制數據進行傳輸,不易捕獲,安全性高app
3.由於接口有字節限制,雖說從理論上GET請求和POST請求均可以進行請求數據和上傳數據,GET請求參數會佔用字節,因此只能進行小數據上傳.而對於POST請求參數在請求體二進制數據中,理論上是無限的.因此,咱們通常使用GET請求獲取數據,POST請求上傳數據.異步
*/網絡傳輸協議
//同步GET編碼
- (IBAction)synGET:(id)sender {url
//1.建立NSURL 對象代理
//(1)獲取urlString
NSString *urlString = [NSString stringWithFormat:@"%@",kPicURL];
//(2)從新編碼
NSString *newStr = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//(3)獲取網址
NSURL *url = [NSURL URLWithString:newStr];
//2.建立NSURLRequest 對象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//3.請求數據
//
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"==%@",response);
NSLog(@"++%@",error);
NSLog(@"--%@",data);
self.imageView.image = [UIImage imageWithData:data];
//解析
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
NSLog(@"%@",dic);
}
//同步POST
- (IBAction)synPOST:(id)sender {
//1.建立NSURL對象
NSString *urlString = [NSString stringWithFormat:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];
NSURL *url = [NSURL URLWithString:urlString];
//2.建立可變請求對象
NSMutableURLRequest *muRequest = [NSMutableURLRequest requestWithURL:url];
//3.設置請求體 和 請求方式
//(1)建立請求參數
NSString *parameterStr = [NSString stringWithFormat:@"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"];
//(2)將請求參數封裝成NSDate對象
NSData *paraData = [parameterStr dataUsingEncoding:NSUTF8StringEncoding];
//設置請求體 請求方式
[muRequest setHTTPBody:paraData];
[muRequest setHTTPMethod:@"POST"];
//4.請求數據
NSData *data = [NSURLConnection sendSynchronousRequest:muRequest returningResponse:nil error:nil];
//...數據處理
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@",dic);
}
//異步GET
- (IBAction)asynGET:(id)sender {
//1.建立NSURL對象
//(1)建立urlString
NSString *urlString = [NSString stringWithFormat:kPicURL];
//(2)從新編碼
NSString *newStr = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//(3)url
NSURL *url = [NSURL URLWithString:newStr];
//2.建立request對象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//3.請求數據
//a 使用代理方法回調進行異步網絡請求
// [NSURLConnection connectionWithRequest:request delegate:self];
//b.使用block進行異步網絡請求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//當數據獲取成功時執行
self.imageView.image = [UIImage imageWithData:data];
}];
}
//異步POST
- (IBAction)asynPOST:(id)sender {
//1.建立NSURL對象
NSString *urlString = [NSString stringWithFormat:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];
NSURL *url = [NSURL URLWithString:urlString];
//2.建立可變的請求對象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//3.設置請求體 和 請求方式
//(1)獲取參數字符串
NSString *paraString = [NSString stringWithFormat:@"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"];
//(2)轉化爲NSData
NSData *data = [paraString dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPBody = data;
request.HTTPMethod = @"POST";
//4.請求
//a 代理方法回調
[NSURLConnection connectionWithRequest:request delegate:self];
//b block
// [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
// NSLog(@"%@",dic);
// }];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - connectionDelegate
//鏈接到服務器
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//當鏈接到服務器時 建立對象
self.data = [NSMutableData data];
NSLog(@"創建鏈接");
}
//獲取數據時
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//拼接數據
[self.data appendData:data];
NSLog(@"獲取數據");
}
//完成請求
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"請求完成");
//數據請求完成 能夠進行數據的操做
self.imageView.image = [UIImage imageWithData:_data];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:_data options:0 error:nil];
NSLog(@"%@",dic);
}
//請求失敗
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"請求失敗");
}
- (void)dealloc {
[_imageView release];
[super dealloc];
}