首页 > 代码库 > 网络编程练习 -- NSURLConnection -- get/post请求
网络编程练习 -- NSURLConnection -- get/post请求
网络编程基础 -- NSURLConnection -- GET请求
LWTViewController.m
//// LWTViewController.m// 网络编程练习 -- NSURLConnection -- get请求//// Created by apple on 14-6-26.// Copyright (c) 2014年 lwt. All rights reserved.//#import "LWTViewController.h"#import "MBProgressHUD+MJ.h"@interface LWTViewController () <NSURLConnectionDataDelegate>@property (weak, nonatomic) IBOutlet UITextField *usernameField;@property (weak, nonatomic) IBOutlet UITextField *pwdField;@property (nonatomic, strong) NSMutableData *data;- (IBAction)loginBtnOnClick;@end@implementation LWTViewController- (void)viewDidLoad{ [super viewDidLoad];}- (IBAction)loginBtnOnClick { NSString *username = self.usernameField.text; NSString *pwd = self.pwdField.text; if (username.length == 0) { [MBProgressHUD showError:@"请输入用户名"]; return; } if (pwd.length == 0) { [MBProgressHUD showError:@"请输入密码"]; return; } NSString *urlStr = [[NSString stringWithFormat:@"http://192.168.1.24:8080/MJServer/login?username=%@&pwd=%@", username, pwd] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString:urlStr]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.timeoutInterval = 5; // [self dataSyncFromRequest:request];// [self dataAsyncFromRequest:request]; [self delegateFromRequest:request]; }/** * 同步请求, 会阻塞线程 */- (void)dataSyncFromRequest : (NSURLRequest *)request{ NSData *data =http://www.mamicode.com/ [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSLog(@"%d",data.length);}/** * 异步请求 */- (void)dataAsyncFromRequest : (NSURLRequest *)request{ [MBProgressHUD showMessage:@"正在拼命加载中..."]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { [MBProgressHUD hideHUD]; if (connectionError) { [MBProgressHUD showError:@"网络繁忙,请稍后再试!!"]; return ; } NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; NSString *error = dict[@"error"]; if (error) { [MBProgressHUD showError:error]; }else { NSString *success = dict[@"success"]; [MBProgressHUD showSuccess:success]; } }];}- (void)delegateFromRequest : (NSURLRequest *)request{ NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self]; [conn start]; [MBProgressHUD showMessage:@"正在拼命加载中..."];}- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ NSLog(@"didFailWithError"); [MBProgressHUD hideHUD]; [MBProgressHUD showError:@"网络繁忙,请稍后再试"];}- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ NSLog(@"didReceiveResponse"); self.data = [NSMutableData data];}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ NSLog(@"didReceiveData"); [self.data appendData:data];}- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ NSLog(@"connectionDidFinishLoading"); [MBProgressHUD hideHUD]; NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:self.data options:NSJSONReadingMutableLeaves error:nil]; NSString *error = dict[@"error"]; if (error) { [MBProgressHUD showError:error]; }else { NSString *success = dict[@"success"]; [MBProgressHUD showSuccess:success]; }}@end
网络编程基础 -- NSURLConnection --POST请求
LWTViewController.m
//// LWTViewController.m// 网络编程练习 -- NSURLConnection -- POST请求//// Created by apple on 14-6-26.// Copyright (c) 2014年 lwt. All rights reserved.//#import "LWTViewController.h"#import "MBProgressHUD+MJ.h"@interface LWTViewController ()@property (weak, nonatomic) IBOutlet UITextField *usernameField;@property (weak, nonatomic) IBOutlet UITextField *pwdField;@property (nonatomic, strong) NSMutableData *data;- (IBAction)loginBtnOnClick;@end@implementation LWTViewController- (void)viewDidLoad{ [super viewDidLoad];}- (IBAction)loginBtnOnClick{ NSString *username = self.usernameField.text; NSString *pwd = self.pwdField.text; if (username.length == 0) { [MBProgressHUD showError:@"请输入用户名"]; return; } if (pwd.length == 0) { [MBProgressHUD showError:@"请输入密码"]; return; } NSURL *url = [NSURL URLWithString:[@"http://192.168.1.24:8080/MJServer/login" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.timeoutInterval = 5; request.HTTPMethod = @"POST"; [request setValue:@"Andriod and iOS" forHTTPHeaderField:@"User-Agent"]; NSString *body = [NSString stringWithFormat:@"username=%@&pwd=%@", username, pwd]; request.HTTPBody = [body dataUsingEncoding:NSUTF8StringEncoding]; [MBProgressHUD showMessage:@"正在拼命加载中..."]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { [MBProgressHUD hideHUD]; if (connectionError) { [MBProgressHUD showError:@"网络繁忙, 请稍后重试!!"]; return ; } NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; NSString *error = dict[@"error"]; if (error) { [MBProgressHUD showError:error]; }else { NSString *success = dict[@"success"]; [MBProgressHUD showSuccess:success]; } }];}@end
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。