簡單說明:建議提交用戶的隱私數據一定要使用Post請求 相對Post請求而言,Get請求的所有參數都直接暴露在URL中,請求的URL一般會記錄在伺服器的訪問日誌中,而伺服器的訪問日誌是黑客攻擊的重點對象之一 用戶的隱私數據如登錄密碼,銀行帳號等 示例代碼 ...
簡單說明:建議提交用戶的隱私數據一定要使用Post請求
相對Post請求而言,Get請求的所有參數都直接暴露在URL中,請求的URL一般會記錄在伺服器的訪問日誌中,而伺服器的訪問日誌是黑客攻擊的重點對象之一
用戶的隱私數據如登錄密碼,銀行帳號等
示例代碼
#define CURRENT_SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width #define CURRENT_SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height - 64) #define BUTTON_WIDTH 80 #define BUTTON_HEIGHT 40 @interface ViewController () //GET 請求 @property(nonatomic,strong) UIButton *getButton; //POST 請求 @property(nonatomic,strong) UIButton *postButton; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _getButton = [[UIButton alloc] initWithFrame:CGRectMake(CURRENT_SCREEN_WIDTH/2 - BUTTON_WIDTH/2, CURRENT_SCREEN_HEIGHT/2 - BUTTON_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT)]; [_getButton setTitle:@"GET 請求" forState:UIControlStateNormal]; [_getButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [_getButton addTarget:self action:@selector(getClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_getButton]; _postButton = [[UIButton alloc] initWithFrame:CGRectMake(CURRENT_SCREEN_WIDTH/2 - BUTTON_WIDTH/2, _getButton.frame.origin.y + _getButton.frame.size.height + 60, BUTTON_WIDTH, BUTTON_HEIGHT)]; [_postButton setTitle:@"POST 請求" forState:UIControlStateNormal]; [_postButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [_postButton addTarget:self action:@selector(postClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_postButton]; } /* get 請求 */ -(void)getClick{ //請求 URL NSString* urlStr = [NSString stringWithFormat:@"https://m.che168.com/beijing/?pvareaid=%d",110100]; //封裝成 NSURL NSURL* url = [NSURL URLWithString:urlStr]; //初始化 請求對象 NSURLRequest* request = [[NSURLRequest alloc] initWithURL:url]; //也可以這樣初始化對象 //NSURLRequest* request = [NSURLRequest requestWithURL:url]; //發送請求 預設為 GET 請求 //1 、獲得會話對象 NSURLSession *session = [NSURLSession sharedSession]; // 2、第一個參數:請求對象 // 第二個參數:completionHandler回調(請求完成【成功|失敗】的回調) // data:響應體信息(期望的數據) // response:響應頭信息,主要是對伺服器端的描述 // error:錯誤信息,如果請求失敗,則error有值 NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { if(!error){ NSLog(@"請求載入成功。。。"); //說明:(此處返回的數據是JSON格式的,因此使用NSJSONSerialization進行反序列化處理) // NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; //如果是字元串則直接取出 NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"GET 請求返回的結果是:%@",[str substringToIndex: 300]); } }]; //執行任務 [dataTask resume]; /* ------------ ios9 之前請求方法,之後改成 NSURLSession 請求 -------------- [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { if(!connectionError){ NSLog(@"載入成功。。。"); NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"載入的內容是:%@",[str substringToIndex:200]); }else{ NSLog(@"載入失敗"); } }]; */ } /* POST 請求 */ -(void)postClick{ NSString *urlStr = [NSString stringWithFormat:@"https://m.che168.com/"]; //轉碼 // stringByAddingPercentEscapesUsingEncoding 只對 `#%^{}[]|\"<> 加空格共14個字元編碼,不包括”&?”等符號), ios9將淘汰 // ios9 以後要換成 stringByAddingPercentEncodingWithAllowedCharacters 這個方法進行轉碼 urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[[NSCharacterSet characterSetWithCharactersInString:@"?!@#$^&%*+,:;='\"`<>()[]{}/\\| "] invertedSet]]; NSURL *url = [NSURL URLWithString:urlStr]; //創建會話對象 NSURLSession *session = [NSURLSession sharedSession]; //創建請求對象 NSMutableURLRequest *request =[[NSMutableURLRequest alloc] initWithURL:url]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:[@"a=1&b=2&c=3&type=json" dataUsingEncoding:NSUTF8StringEncoding]]; //根據會話對象創建一個 Task(發送請求) NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { if(!error){ //8.解析數據 // NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; // NSLog(@"%@",dict); NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"POST 載入的內容是:%@",[str substringToIndex:200]); }else{ NSLog(@"請求發生錯誤:%@", [error description]); } }]; [dataTask resume]; //執行任務 }