네트워크 get 생 성 요청

두 가지 상용 방식 1. 비동기 요청
// 0.          URL
    //   url:
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:
@"http://122.33.44.55:8586/login_check_byPhone?userName=%@&passWord=%@",userName,pwd]];
    //NSLog(@"url:%@",url);
    
    // 1.      
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    // 2.    ,                   
   [NSURLConnection sendAsynchronousRequest:request  //    
                                       queue:[[NSOperationQueue alloc] init] //       
                           completionHandler:^(NSURLResponse * _Nullable //  Block      
                                               response, NSData * _Nullable data, //      
                                               NSError * _Nullable connectionError) { //     
        
        // 3.      
        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@",str);
        
        // HUD             
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            if ([str containsString:@"success"]) {
                [SVProgressHUD showSuccessWithStatus:@"    "];
            }else{
                [SVProgressHUD showErrorWithStatus:@"    "];
            }
        }];
    }];

2. 대리 방법
//   url:
    NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=123&pwd=123"];
    //NSLog(@"url:%@",url);
    
    // 1.      
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    // 2.      
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

#pragma mark - NSURLConnectionDataDelegate           
/**
 *      
 *
 *  @param connection
 *  @param error
 */
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"%s,%d-----%@",__func__,__LINE__,error);
}

/**
 *        ,       ,        
 *
 *  @param connection
 *  @param data
 */
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%s,%d----%@",__func__,__LINE__,str);
    
}

/**
 *          
 *
 *  @param connection
 *  @param response
 */
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    NSLog(@"%s,%d----%@",__func__,__LINE__,response);
    
}

/**
 *           
 *
 *  @param connection
 */
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSLog(@"%s,%d",__func__,__LINE__);
    
}

좋은 웹페이지 즐겨찾기