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];