1.NSURLConnection 异步请求。
举例,下载图片。
- (void)viewDidLoad {
[superviewDidLoad];
NSURL *urlString = [NSURL URLWithString:@"http://www.pptbz.com/pptpic/UploadFiles_6909/201110/20111014111307895.jpg"]; //创建请求路径。
NSOperationQueue *queue = [[NSOperationQueuealloc]init];
NSURLRequest *request = [NSURLRequestrequestWithURL:urlString];//创建请求对象。
[NSURLConnection sendAsynchronousRequest:request queue:queuecompletionHandler:^(NSURLResponse *_Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
UIImage *image = [UIImageimageWithData:data];
NSLog(@"%@ --- response :%@",image,response);//打印response查看响应体的内容。
}];//发送请求方法。
/* Designated initializer */
//主要有三种发起请求的方法
- (nullable instancetype)initWithRequest:(NSURLRequest *)request delegate:(nullableid)delegate startImmediately:(BOOL)startImmediately;//这个方法不是立即启动需要手动启动,start。
- (nullable instancetype)initWithRequest:(NSURLRequest *)request delegate:(nullableid)delegate;
+ (nullable NSURLConnection*)connectionWithRequest:(NSURLRequest *)request delegate:(nullableid)delegate ;
}
2.Response响应体的内容。
{ URL: http://www.pptbz.com/pptpic/UploadFiles_6909/201110/20111014111307895.jpg } { status code: 200, headers {
"Accept-Ranges" = bytes;
Age = 1;
Connection = "keep-alive";
"Content-Length" = 66572;// 图片的大小。
"Content-Type" = "image/jpeg";//传输的文件格式。
Date = "Tue, 02 Aug 2016 03:37:02 GMT";
Etag = "\"0ba55321f8acc1:2ba\"";
"Last-Modified" = "Fri, 14 Oct 2011 03:13:08 GMT";
Server = "Microsoft-IIS/6.0";
"X-Via" = "1.1 xzai40:0 (Cdn Cache Server V2.0), 1.1 wt197:10 (Cdn Cache Server V2.0)";
} }
3.NSURLConnection GET 请求方法:
NSURL *urlString = [NSURL URLWithString:@"http://www.pptbz.com?username=%@&pwd=%@",self.name.text,self.pwd.text];
4.NSURLConnection 请求代理方法:
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"%s",__func__);//超时会执行这个方法。
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(nonnullNSData *)data{
_responseData = [NSMutableDatadata];
[_responseData appendData:data];
[self showProgress];
NSLog(@"%s,%@",__func__,data);
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(nonnullNSURLResponse *)response{
_totalData = response.expectedContentLength;
NSLog(@"%s,%lld",__func__,response.expectedContentLength);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"%s",__func__);
//请求完成在这里更新数据。
[self showImage:_responseData];
}
-(void)showProgress{
_progress = [[UIProgressViewalloc]initWithFrame:CGRectMake(10,20, self.view.bounds.size.width,20)];
_progress.progressViewStyle =UIProgressViewStyleDefault;
_progress.progress = [self.responseDatalength]*1.0 / _totalData;
[self.viewaddSubview:_progress];
}
5.NSURLConnection POST请求:
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:urlString];
request.HTTPMethod = @"POST";
request.HTTPBody = [@"username=&pwd="dataUsingEncoding:NSUTF8StringEncoding];
request.timeoutInterval =5.0;
[request setValue:@"iOS 9.0"forHTTPHeaderField:@"User-Agent"];//告诉服务器该设备是什么类型。
此方法可以用在SOAP请求,指定请求方法和请求的数据类型。
6.中文转码,UTF8.
7.取消请求,cancel。
8.NSString -> NSData
NSData *data = [@"xxxx" dataUsingEncoding:NSUTF8StringEncoding];
NSString *sting = [[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding];