一、同步GET
// 1.將網址初始化成一個OC字符串對象
NSString *urlStr = [NSString stringWithFormat:@"%@?wewe=%@", @"111", @"222"];
// 2.如果網址中存在中文,進行URLEncodeNSString *newUrlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];// 3.構建網絡URL對象, NSURLNSURL *url = [NSURL URLWithString:newUrlStr];// 4.創建網絡請求NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];// 5.創建同步鏈接NSURLResponse *response = nil;NSError *error = nil;NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];// 1.將網址初始化成一個OC字符串對象
NSString *urlStr = [NSString stringWithFormat:@"%@?wewe=%@", @"111", @"222"];
// 2.如果網址中存在中文,進行URLEncodeNSString *newUrlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];// 3.構建網絡URL對象, NSURLNSURL *url = [NSURL URLWithString:newUrlStr];// 4.創建網絡請求NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];[NSURLConnection sendAsynchronousRequest:requst queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { //your code }]// 異步連接(形式2)// 服務器接收到請求時- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{}// 當收到服務器返回的數據時觸發, 返回的可能是資源片段,需要拼接資源片段- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{}// 當服務器返回所有數據時觸發, 數據返回完畢- (void)connectionDidFinishLoading:(NSURLConnection *)connection{}// 請求數據失敗時觸發- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ NSLog(@"%@", error);}// 1.將網址初始化成一個OC字符串對象
NSString *urlStr = [NSString stringWithFormat:@"%@?wewe=%@", @"111", @"222"];
// 2.如果網址中存在中文,進行URLEncodeNSString *newUrlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];// 3.構建網絡URL對象, NSURLNSURL *url = [NSURL URLWithString:newUrlStr];// 4.創建請求NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];// 5.創建參數字符串對象NSString *parmStr = @"method=album.channel.get&appKey=myKey&format=json&channel=t&pageNo=1&pageSize=10";// 6.將字符串轉為NSData對象NSData *PRamData = [parmStr dataUsingEncoding:NSUTF8StringEncoding];// 7.設置請求體// 8.設置請求方式[request setHTTPMethod:@"POST"]; // 9.創建同步鏈接 NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];// 1.將網址初始化成一個OC字符串對象
NSString *urlStr = [NSString stringWithFormat:@"%@?wewe=%@", @"111", @"222"];
// 2.如果網址中存在中文,進行URLEncodeNSString *newUrlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];// 3.構建網絡URL對象, NSURLNSURL *url = [NSURL URLWithString:newUrlStr];// 4.創建請求NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];// 5.創建參數字符串對象NSString *parmStr = @"method=album.channel.get&appKey=myKey&format=json&channel=t&pageNo=1&pageSize=10";// 6.將字符串轉為NSData對象NSData *pramData = [parmStr dataUsingEncoding:NSUTF8StringEncoding];// 7.設置請求體[request setHTTPBody:pramData];// 8.設置請求方式[request setHTTPMethod:@"POST"];// 9.創建異步連接(形式2)[NSURLConnection connectionWithRequest:request delegate:self];GET請求和POST請求的區別:
1. GET請求的接口會包含參數部分,參數會作為網址的一部分,服務器地址與參數之間通過 ? 來間隔。POST請求會將服務器地址與參數分開,請求接口中只有服務器地址,而參數會作為請求的一部分,提交后臺服務器。
2. GET請求參數會出現在接口中,不安全。而POST請求相對安全。
3.雖然GET請求和POST請求都可以用來請求和提交數據,但是一般的GET多用于從后臺請求數據,POST多用于向后臺提交數據。
同步和異步的區別:
同步鏈接:主線程去請求數據,當數據請求完畢之前,其他線程一律不響應,會造成程序就假死現象。
異步鏈接:會單獨開一個線程去處理網絡請求,主線程依然處于可交互狀態,程序運行流暢。
|
新聞熱點
疑難解答