同步请求与异步请求

@interface ViewController ()
@property (weak, nonatomic) IBOutletUITextField *usernameTextField;
@property (weak, nonatomic) IBOutletUITextField *passwordTextField;
@property (strong,nonatomic) NSMutableData *fullData;
@property (weak, nonatomic) IBOutletUIImageView *imageView;

@end

@implementation ViewController
//同步
- (
IBAction)loginBySync:(UIButton *)sender
{
   
//1、网络连接地址
   
NSString *strURL =@"http://192.168.191.4/usermanager/0.png";
   
NSURL *url = [NSURLURLWithString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
   
//2、建立请求对象
   
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
    request.
timeoutInterval = 1;
    request.
HTTPMethod = @"post";
   
   
NSString *strBody = [NSStringstringWithFormat:@"submit =1&username = %@&password =%@",self.usernameTextField.text,self.passwordTextField.text];
   
    request.
HTTPBody = [strBody dataUsingEncoding:NSUTF8StringEncoding];
   
   
//request.HTTPBody = [[NSData alloc]initWithBytes:[strBody UTF8String] length:strBody.length];
   
//3、建立连接,发送请求
   
NSURLResponse *response = nil;
   
NSError *error = nil;
   
NSData *data = [NSURLConnectionsendSynchronousRequest:request returningResponse:&response error:&error];
   
if (error ==nil)
    {
       
unsignedlong encode = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
       
NSString *str = [[NSStringalloc]initWithData:data encoding:encode];
       
NSLog(@"接收到数据:%@",str);
    }
   
else
    {
       
NSLog(@"连接出错%@",error);
   
    }
   
}
//异步
- (
IBAction)loginAsync:(UIButton *)sender
{
   
//1、网络连接地址
   
NSString *strURL =@"http://192.168.191.4/usermanager/0.png";
   
NSURL *url = [NSURLURLWithString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
   
//2、建立请求对象
   
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
    request.
timeoutInterval = 1;
    request.
HTTPMethod = @"post";
   
   
NSString *strBody = [NSStringstringWithFormat:@"submit =1&username = %@&password =%@",self.usernameTextField.text,self.passwordTextField.text];
   
    request.
HTTPBody = [strBody dataUsingEncoding:NSUTF8StringEncoding];
   
   
//request.HTTPBody = [[NSData alloc]initWithBytes:[strBody UTF8String] length:strBody.length];
   
//3、建立连接,发送请求
    [
NSURLConnectionsendAsynchronousRequest:request queue:[NSOperationQueuemainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
       
if (connectionError ==nil)
        {
           
unsignedlong encode = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
           
NSString *str = [[NSStringalloc]initWithData:data encoding:encode];
           
NSLog(@"接收到数据:%@",str);
        }
       
else
        {
           
NSLog(@"连接出错%@",connectionError);
           
        }
    }];
}

//get
- (
IBAction)LoginByget:(UIButton *)sender
{
   
//1、网络连接地址
   
self.fullData = [[NSMutableDataalloc]init];

   
NSString *strURL = [NSStringstringWithFormat:@"http://192.168.191.4/usermanager/login.php?username=%@&password=%@",self.usernameTextField.text,self.passwordTextField.text];
   
//NSString *strURL = [NSString stringWithFormat:@"http://192.168.191.4/usermanager/0.png";
   
NSURL *url = [NSURLURLWithString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
   
//2、建立请求对象
   
NSURLRequest *request = [NSURLRequestrequestWithURL:url];
   
//3、建立连接,发送请求
   
NSURLConnection *conn = [NSURLConnectionconnectionWithRequest:request delegate:self];
    [conn
start];
   
}
//post
- (
IBAction)LoginBypost:(UIButton *)sender
{
   
   
self.fullData = [[NSMutableDataalloc]init];
   
//1、网络连接地址
   
NSString *strURL =@"http://192.168.191.4/usermanager/0.png";
   
NSURL *url = [NSURLURLWithString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
   
//2、建立请求对象
   
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
   
    request.
HTTPMethod = @"post";
   
   
NSString *strBody = [NSStringstringWithFormat:@"submit =1&username = %@&password =%@",self.usernameTextField.text,self.passwordTextField.text];
   
    request.
HTTPBody = [strBody dataUsingEncoding:NSUTF8StringEncoding];
   
   
//request.HTTPBody = [[NSData alloc]initWithBytes:[strBody UTF8String] length:strBody.length];
   
//3、建立连接,发送请求
   
NSURLConnection *conn = [NSURLConnectionconnectionWithRequest:request delegate:self];
    [conn
start];
   
}

- (
void)viewDidLoad
{
    [
superviewDidLoad];
}

#pragma mark - NSURLConnectionDataDelegate方法
#pragma mark
收到数据时调用的方法,可能收到多个数据包,在这个方法中完成数据包的拼接
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [
self.fullDataappendData:data];


}
#pragma mark 数据接收完毕时的方法,对接收到的数据进行解析,进行相应的处理
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
   
unsignedlong encode = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
   
NSString *str = [[NSStringalloc]initWithData:self.fullDataencoding:encode];
   
NSLog(@"接收到数据:%@",str);
   
//    UIImage *image = [UIImage imageWithData:self.fullData];
//    self.imageView.image = image;

}
-(
void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{

   
NSLog(@"连接出错:%@",error);
}

你可能感兴趣的:(XML)