iOS-->有關NSURLConnection

xcode.png
NSUrlConnection
使用NSURLConnection傳送請求的步驟:
- ①設定請求路徑
- ②建立請求物件(預設是GET請求,且已經預設包含了請求頭)
- ③使用NSURLConnection傳送網路請求
- ④接收到伺服器的響應後,解析響應體
使用NSURLConnection傳送Get請求
- 1.傳送同步Get請求
-(void)sendSyncRequest{
//確定請求路徑
NSURL *url=[NSURL URLWithString:@"http://XXXX%B4%E5%81%A5&pwd=2uji&type=JSON"];
//建立請求物件
NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];
NSURLResponse *response=nil;
NSError *error=nil;
//使用NSURLConnection傳送請求
NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//解析從伺服器返回的資料
NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
NSLog(@"%@",[NSThread currentThread]);
}
- 2.傳送非同步Get請求
-(void)sendAsyncRequest{
//確定請求路徑
NSURL *url=[NSURL URLWithString:@"http://XXXXX/login?username=XXX&pwd=XXX&type=JSON"];
//建立請求物件
NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];
//使用NSURLConnection傳送請求
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//解析從伺服器返回的資料
NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
//列印當前執行緒
NSLog(@"%@",[NSThread currentThread]);
//響應頭的真實型別為NSHTTPURLResponse
NSHTTPURLResponse *response1=(NSHTTPURLResponse *)response;
NSLog(@"%zd",response1.statusCode);
NSLog(@"%@",response1.allHeaderFields);
}];
}
- 3.使用代理髮送請求
-(void)requestDelegate{
//確定請求路徑
NSURL *url=[NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"];
//建立請求物件
NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];
// NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
//如果把最後一個引數設定為NO,就表示不立即傳送網路請求,而是在需要的時候呼叫Start方法來傳送
//設定代理
NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:NO];
[connection start];
}
//接收到伺服器響應的時候會呼叫此方法
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
self.data=[NSMutableData data];
}
//接收到從伺服器返回的資料的時候呼叫 (會多次呼叫)
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.data appendData:data];
}
//請求完成的時候呼叫該方法
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"%@",[[NSString alloc]initWithData:self.data encoding:NSUTF8StringEncoding]);
}
使用NSURLConnection傳送Post請求
-(void)post{
//確定請求路徑
NSURL *url=[NSURL URLWithString:@"http://x/login"];
//建立可變的請求物件
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:url];
//設定請求方法為POST
request.HTTPMethod=@"POST";
//設定請求體
request.HTTPBody=[@"username=xxx&pwd=xxxxx&type=JSON"dataUsingEncoding:NSUTF8StringEncoding];
//設定超時時間
request.timeoutInterval=15;
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
}];
URL中文轉碼處理
如果URL中包含中文,則在傳送請求的時候會出錯,為了解決這一問題,我們需要對含有中文的URL做一個轉碼處理:
NSString *urlStr = @"http://120.25.226.186:32812/login2?username=哈哈&pwd=xxxx&type=JSON";
NSLog(@"轉換前:%@",urlStr);
//中文轉碼處理
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"轉換後:%@",urlStr);
NSURL *url = [NSURL URLWithString:urlStr];
相關文章
- ios--二維碼生成iOS
- iOS--利用Fastlane實現自動化打包iOSAST
- iOS開發·網路請求大總結(NSURLConnection,NSURLSession,AFNetworking)iOSSession
- iOS--手把手教你一步一步完成搖骰子動畫iOS動畫
- 有關 Stories
- 有關requestAnimationFramerequestAnimationFrame
- 有關自己
- JavaScript --有關提升JavaScript
- 有關模型關聯的問題模型
- 有關React面試React面試
- Rust 有問有答之 use 關鍵字Rust
- 有關箭頭函式函式
- 有關Weak Head Normal FormORM
- 有關Kafka的那些事Kafka
- 有關git cherry-pickGit
- 有關元件的補充~~~~~~~元件
- 有關動態規劃動態規劃
- 智雲通CRM:銷售過程中,沒有關心就沒有關係?
- 有關float、position:absolute及a元素
- 有關this指標指向問題指標
- 多值依賴的有關概念
- 有關Editor的記錄(一)
- 有關HTTP的PATCH提交方式HTTP
- 有關https的SSL加密方式HTTP加密
- 有關 ThreadLocal 的一切thread
- 有關動態規劃的相關優化思想動態規劃優化
- 有沒有關於物聯網創新的點子
- 嘉峪關哪裡有開票-嘉峪關開票
- SkyDrive與OneDrive有什麼關係?
- Linux技術相關命令有哪些Linux
- 有關效能優化的感悟.2021優化
- EMI/EMS/EMC有什麼關係?
- 有關GPIO的使用方法教程
- Unity 的 Surface Shader有關記錄Unity
- 有沒有關於敏捷測試的書籍推薦呢?敏捷測試
- 羊了個羊有第三關嗎?微信小程式羊了個羊有第三關嗎微信小程式
- 有關js各種陣列遍歷JS陣列
- 有關資料驗證的原則
- 【vuejs】有關vue的一些小技巧VueJS