国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

NSURLConnection三種請求方式

2019-11-06 06:41:03
字體:
供稿:網(wǎng)友

1.發(fā)送同步請求(返回NSData數(shù)據(jù))

[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

在當(dāng)前線程執(zhí)行,返回NSData數(shù)據(jù)

2.發(fā)送異步請求(利用block)

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { NSLog(@"返回數(shù)據(jù):%@",data); }];

在異步線程執(zhí)行,請求成功后,在block回調(diào)函數(shù)里拿到返回的數(shù)據(jù)體

3.發(fā)送異步請求(利用代理)

NSURLConnection *connection = [NSURLConnection alloc] init];connection.delegate = self;

在異步線程執(zhí)行,請求成功后,調(diào)用代理的相關(guān)方法拿到相應(yīng)的數(shù)據(jù),這種方法適合大文件分段下載。

/**1.接收到服務(wù)器的響應(yīng) */- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{}/**2.接收到服務(wù)器的數(shù)據(jù)*/- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{}/**3.服務(wù)器的數(shù)據(jù)接收完畢*/- (void)connectionDidFinishLoading:(NSURLConnection *)connection{}/**4.請求錯誤*/- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{}

一、GET請求

1.如果有參數(shù),直接拼接在URL后面,用?隔開,且每個參數(shù)用&分開.

利用NSURLConnection不需要包含請求行和請求頭,底層以及包裝好,且默認(rèn)就是get請求.我們只需加載指定URL資源,創(chuàng)建請求,發(fā)送請求即可。

示例程序:訪問服務(wù)器登錄界面

//1.資源字符串 NSString *urlStr = @"http://127.0.0.1/login.php?name=張三&passWord=1234"; //如果字符串里面含有中文要進(jìn)行轉(zhuǎn)碼 urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //2.創(chuàng)建資源路徑 NSURL *url = [NSURL URLWithString:urlStr]; //3.創(chuàng)建請求 NSURLRequest *request = [NSURLRequest requestWithURL:url]; //4.發(fā)送請求 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { NSLog(@"%@",data); }];

二、post請求

利用NSURLConnection,由于默認(rèn)是GET請求,所以要申明請求方法為POST,且需要封裝

//1.資源字符串 NSString *urlStr = @"http://127.0.0.1/login.php"; //如果字符串里面含有中文要進(jìn)行轉(zhuǎn)碼 //urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //2.設(shè)置請求路徑 NSURL *url = [NSURL URLWithString:urlStr]; //3.創(chuàng)建請求 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 默認(rèn)就是GET請求 request.timeoutInterval = 5; // 設(shè)置請求超時 request.HTTPMethod = @"POST"; // 設(shè)置為POST請求 //4.設(shè)置請求體 NSString *param = [NSString stringWithFormat:@"name=%@&password=%@",name, password];//name和password一般從輸入框取得 request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding]; //5.發(fā)送請求 NSOperationQueue *queue = [NSOperationQueue mainQueue]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { // 當(dāng)請求結(jié)束的時候調(diào)用 NSLog(@"返回數(shù)據(jù)-%@",data); }];

三、head請求

其實(shí)head請求和get請求差不多,只不過是服務(wù)器只返回響應(yīng)頭,不返回具體數(shù)據(jù),這可以幫助我們后面講解下載文件時如何獲得文件大小

NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/images/xhr.png"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @"HEAD"; NSURLResponse *response = nil; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { NSLog(@"文件長度-%lld",response.expectedContentLength; }];

相比較而言,post請求比get請求安全,get請求所有數(shù)據(jù)暴露在URL中,post數(shù)據(jù)封裝在請求體中, 不過通過Firefox也能查看到post的請求體,對于網(wǎng)頁服務(wù)器開發(fā),firefox是個利器,強(qiáng)烈推薦。

對于數(shù)據(jù)安全這塊不用擔(dān)心,后面會講解如何對數(shù)據(jù)進(jìn)行加密處理.

原文:http://www.cnblogs.com/wc85328/p/4250918.html


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 盐亭县| 五莲县| 太仓市| 淮南市| 会同县| 苏尼特右旗| 改则县| 孟津县| 内黄县| 东阳市| 揭西县| 新泰市| 香港| 南昌市| 永康市| 临武县| 舟曲县| 郁南县| 苍山县| 顺昌县| 铅山县| 河源市| 六盘水市| 澄江县| 绥中县| 郓城县| 衡南县| 讷河市| 铁岭市| 措勤县| 淳安县| 宁蒗| 明水县| 秭归县| 武清区| 湄潭县| 茶陵县| 信丰县| 循化| 台东市| 广河县|