Iphone访问WCF服务 之 ASIHTTPRequest
1 #import "LyViewController.h" 2 #import "ASIHTTPRequest.h" 3 4 @interfaceLyViewController () 5 @propertyconstNSString *wcfConn; 6 @property NSString *userNameText; 7 @property NSString *userPwdText; 8 9 @end 10 @implementation LyViewController 11 12 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 13 { 14 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 15 if (self) { 16 } 17 returnself; 18 } 19 20 - (void)viewDidLoad 21 { 22 [superviewDidLoad]; 23 self.wcfConn=@"http://192.168.1.3:6001/IPhone.I520.svc"; //服务器连接 24 } 25 26 #pragma mark --登录 27 - (IBAction)Login:(id)sender { 28 [self getParms]; 29 NSString *conn=[NSStringstringWithFormat:@"%@/%@/%@/%@",self.wcfConn,@"UserLogin",self.userNameText,self.userPwdText]; 30 NSLog(@"%@",conn); 31 NSURL *url=[NSURLURLWithString:conn]; 32 ASIHTTPRequest *request=[ASIHTTPRequestrequestWithURL:url]; 33 [request setRequestMethod:@"POST"]; 34 [request startSynchronous]; 35 NSError *error=[request error]; 36 if (!error) { 37 NSString *response=[request responseString]; 38 UIAlertView *alert=[[UIAlertViewalloc]initWithTitle:@"Login"message:response delegate:nilcancelButtonTitle:@"Ok"otherButtonTitles:nil, nil]; 39 [alert show]; 40 [alert release]; 41 } 42 [selfregiestFocuse]; 43 } 44 #pragma mark --注册 45 46 - (IBAction)Regiest:(id)sender { 47 [self getParms]; 48 NSString *conn=[NSStringstringWithFormat:@"%@/%@/%@/%@",self.wcfConn,@"UserRegist",self.userNameText,self.userPwdText]; 49 NSLog(@"%@",conn); 50 NSURL *url=[NSURLURLWithString:conn]; 51 ASIHTTPRequest *request=[ASIHTTPRequestrequestWithURL:url]; 52 [request setRequestMethod:@"POST"]; 53 [request startSynchronous]; 54 NSError *error=[request error]; 55 if (!error) { 56 NSString *response=[request responseString]; 57 UIAlertView *alert=[[UIAlertViewalloc]initWithTitle:@"Regiest"message:response delegate:nilcancelButtonTitle:@"Ok"otherButtonTitles:nil, nil]; 58 [alert show]; 59 [alert release]; 60 } 61 [selfregiestFocuse]; 62 } 63 64 #pragma mark --读取信息 65 66 - (IBAction)Getdata:(id)sender { 67 [self getParms]; 68 NSString *conn=[NSStringstringWithFormat:@"%@/%@/%@/%@",self.wcfConn,@"GetUserInfo",self.userNameText,self.userPwdText]; 69 NSLog(@"%@",conn); 70 NSURL *url=[NSURLURLWithString:conn]; 71 ASIHTTPRequest *request=[ASIHTTPRequestrequestWithURL:url]; 72 [request setRequestMethod:@"POST"]; 73 [request startSynchronous]; 74 NSError *error=[request error]; 75 if (!error) { 76 NSString *response=[request responseString]; 77 78 UIAlertView *alert=[[UIAlertViewalloc]initWithTitle:@"GetUserInfo"message:response delegate:nilcancelButtonTitle:@"Ok"otherButtonTitles:nil , nil]; 79 [alert show]; 80 [alert release]; 81 } 82 [selfregiestFocuse]; 83 } 84 85 #pragma mark --异步获取登录信息 86 -(IBAction)GetDataBackUseInfo:(id)sender{ 87 [self getParms]; 88 NSString *conn=[NSStringstringWithFormat:@"%@/%@/%@/%@",self.wcfConn,@"GetUserInfo",self.userNameText,self.userPwdText]; 89 NSLog(@"%@",conn); 90 NSURL *url=[NSURLURLWithString:conn]; 91 ASIHTTPRequest *request=[ASIHTTPRequestrequestWithURL:url]; 92 [request setRequestMethod:@"POST"]; //指定读取方法 93 [request setDelegate:self]; 94 [request startSynchronous]; 95 } 96 97 #pragma mark --当以文本读取返回内容时,调用这方法 98 -(void)requestFinished:(ASIHTTPRequest *)request{ 99 NSString *response=[request responseString]; 100 UIAlertView *alert=[[UIAlertViewalloc]initWithTitle:@"GetUserInfo"message:response delegate:nilcancelButtonTitle:@"Ok"otherButtonTitles:nil , nil]; 101 102 [alert show]; 103 [alert release]; 104 } 105 106 #pragma mark --当以二进制形式读取返回内容时,调用这方法 107 -(void)requestFinish:(ASIHTTPRequest *)request{ 108 109 NSData *response=[request responseData]; 110 111 UIAlertView *alert=[[UIAlertViewalloc]initWithTitle:@"GetUserInfo"message:response delegate:nilcancelButtonTitle:@"Ok"otherButtonTitles:nil , nil]; 112 113 [alert show]; 114 [alert release]; 115 } 116 117 -(void)requestFailed:(ASIHTTPRequest *)request{ 118 NSError *error=[request error]; 119 } 120 121 #pragma mark --取消第一响应都 122 -(void)regiestFocuse{ 123 //[self.userName resignFirstResponder]; 124 //[self.userPwd resignFirstResponder]; 125 [self.viewendEditing:YES]; 126 } 127 128 -(void)getParms{ 129 self.userNameText=self.userName.text; 130 self.userPwdText=self.userPwd.text; 131 } 132 133 - (void)didReceiveMemoryWarning 134 { 135 [superdidReceiveMemoryWarning]; 136 } 137 138 - (void)dealloc { 139 self.wcfConn=NULL; 140 [super dealloc]; 141 } 142 @end 143 144 145 注: 146 使用ASIHTTPRequest另添加框架: 147 AssetsLibrary,MobileCoreServices,SystemConfiguration, 148 CFNetwork,UIKIT,Foundation,CoreGraphics