iOS笔记网络--get请求和post请求
2015-10-12 22:22
323 查看
申明:此为本人学习笔记,若有纰漏错误之处的可留言共同探讨
/*
get请求的特点 (当客户端要从服务器中读取文档时,当点击网页上的链接或者通过在浏览器的地址栏输入网址来浏览网页的,使用的都是get方式,它是最常见的一种请求数据的方式)
1.传输的长度有限制
2.不适合传输私密数据
3.不包含请求数据部分
post请求的特点(post方法将请求参数封装在http请求数据中,以名称/值的形式出现,可以传输大量数据,这样post方式对传送的数据大小没有限制,而且也不会显示在url中,大多用于页面的表单中)
1.理论上传输长度不受限制
2.传输数据相对安全
使用系统方法发送get网络请求
1
创建url字符串
2
创建url,将刚刚创建的字符创转变成URL
3
创建一个URLRequest对象,request的URL使用刚刚的URL
4
使用NSURLConnection方法
发送请求 使用上面的request
代理是自己
类似于
衣服放在箱包 人提着箱包,人开车走了。
衣服就是字符串、箱包就是URL、人就是Request、车就是NSURLConnection,开车的是自己。
post步骤
1根据URL创建一个请求对象
2参数拼接
3转码 把拼接好的字符串转化成data
4设置请求包体(往请求数据区添加数据)
5默认发get请求
6设置超时时间 秒
7发送请求(跟服务器建立一个连接) 发送同步请求(或异步请求)
8去掉首尾空格换行符
9判断登录 成功提示成功 失败提示错误信息
*/
// 下面是本人练习的一个登陆功能,利用服务器发送请求数据去注册和登陆用户
附上项目代码链接:http://yunpan.cn/cFKjQBUEImmNw (提取码:da8c)
@interface MainViewController : UIViewController<NSURLConnectionDataDelegate,NSURLConnectionDelegate>
{
UITextField *userName;
UITextField *password;
NSMutableData *_data;
}
@end
//
// MainViewController.m
// LoginDemo
//
// Created by xinguo on 15/10/12.
// Copyright (c) 2015年 2015-10-12. All rights reserved.
//
#import "MainViewController.h"
#import "ZCViewController.h"
#import "LGViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
self.title = @"登录界面";
// label
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 30)];
label.text = @"用户名:";
[self.view addSubview: label];
// label
UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 100, 30)];
label1.text = @"密码:";
[self.view addSubview: label1];
userName = [[UITextField alloc]initWithFrame:CGRectMake(200, 100, 100, 30)];
userName.layer.borderWidth = 1;
[self.view addSubview:userName];
password = [[UITextField alloc]initWithFrame:CGRectMake(200, 200, 100, 30)];
password.layer.borderWidth = 1;
[self.view addSubview:password];
// 注册
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100 , 300, 100, 40);
button.backgroundColor = [UIColor blueColor];
[self.view addSubview:button];
[button setTitle:@"注册" forState:UIControlStateNormal];
[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
// 登录
UIButton *login = [UIButton buttonWithType:UIButtonTypeCustom];
login.frame = CGRectMake(210 , 300, 100, 40);
login.backgroundColor = [UIColor blueColor];
[self.view addSubview:login];
[login setTitle:@"登录" forState:UIControlStateNormal];
[login addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
}
// 注册
-(void)click
{
ZCViewController *zcVC = [[ZCViewController alloc]init];
[self.navigationController pushViewController:zcVC animated:YES];
NSLog(@"zhuce");
}
// 登录
-(void)loginClick
{
/*
post步骤
1根据URL创建一个请求对象
2参数拼接
3转码 把拼接好的字符串转化成data
4设置请求包体(往请求数据区添加数据)
5默认发get请求
6设置超时时间 秒
7发送请求(跟服务器建立一个连接) 发送同步请求
8去掉首尾空格换行符
9判断登录 成功提示成功 失败提示错误信息
*/
NSString *urlString = @"http://127.0.0.1:8080/A/a";
// 1根据URL创建一个请求对象
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
// 2参数拼接
NSString *parameter = [NSString stringWithFormat:@"command=1&name=%@&psw=%@",userName.text,password.text];
NSLog(@"%@",parameter);
// 3转码 把拼接好的字符串转化成data
NSData *data = [parameter dataUsingEncoding:NSUTF8StringEncoding];
// 4设置请求包体(往请求数据区添加数据)
[request setHTTPBody:data];
// 5默认发get请求
[request setHTTPMethod:@"post"];
// 6设置超时时间 秒
[request setTimeoutInterval:20];
// NSLog(@"%@",request);
// 7发送请求(跟服务器建立一个连接) 发送同步请求
NSURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *str =[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
// 8去掉首尾空格换行符
NSString *newStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
// 判断登录 成功提示成功 失败提示错误信息
if ([newStr isEqualToString:@"登录成功!"])
{
LGViewController *lg = [[LGViewController alloc]init];
[self.navigationController pushViewController:lg animated:YES];
NSLog(@"lgoin");
}else
{
// 提示出错
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"错误提示" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
[alertView show] ;
}
}
//收到响应,意味着数据马上就要来了
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)respons
{
NSLog(@"收到响应");
_data = [[NSMutableData alloc]init];
}
//收到数据,这个方法会被调用多次,因为数据量太大的话,服务器不会一次性把数据全给你
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"收到数据");
[_data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"请求完成");
//把data转化成字符串
// NSString *str = [[NSString alloc]initWithData:_data encoding:NSUTF8StringEncoding];
// NSLog(@"%@",str);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"请求失败");
}
// 取消文本框编辑
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//
// AppDelegate.m
// LoginDemo
//
// Created by xinguo on 15/10/12.
// Copyright (c) 2015年 2015-10-12. All rights reserved.
//
#import "AppDelegate.h"
#import "MainViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self.window makeKeyAndVisible];
MainViewController *mainVC = [[MainViewController alloc]init];
UINavigationController *mainNC = [[UINavigationController alloc]initWithRootViewController:mainVC];
self.window.rootViewController = mainNC;
// Override point for customization after application launch.
return YES;
}
//
// ZCViewController.h
// LoginDemo
//
// Created by xinguo on 15/10/12.
// Copyright (c) 2015年 2015-10-12. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ZCViewController : UIViewController<NSURLConnectionDataDelegate,NSURLConnectionDelegate>
{
UITextField *userT;
UITextField *passwordT;
UITextField *passwordSureT;
UITextField *nameT;
UITextField *emailT;
NSMutableData *_data;
}
@end
//
// ZCViewController.m
// LoginDemo
//
// Created by xinguo on 15/10/12.
// Copyright (c) 2015年 2015-10-12. All rights reserved.
//
#import "ZCViewController.h"
@interface ZCViewController ()
@end
@implementation ZCViewController
// 加载
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// userL
UILabel * userL = [[UILabel alloc]initWithFrame:CGRectMake(100,100, 100, 30)];
userL.text = @"用户名:";
[self.view addSubview: userL];
userT = [[UITextField alloc]initWithFrame:CGRectMake(200, 100, 100, 30)];
userT.layer.borderWidth = 1;
[self.view addSubview:userT];
// password
UILabel * passwordL = [[UILabel alloc]initWithFrame:CGRectMake(100,150, 100, 30)];
passwordL.text = @"密码:";
[self.view addSubview: passwordL];
passwordT = [[UITextField alloc]initWithFrame:CGRectMake(200, 150, 100, 30)];
passwordT.layer.borderWidth = 1;
[self.view addSubview:passwordT];
// passwordSure
UILabel * passwordSureL = [[UILabel alloc]initWithFrame:CGRectMake(100,200, 100, 30)];
passwordSureL.text = @"密码确认:";
[self.view addSubview: passwordSureL];
passwordSureT = [[UITextField alloc]initWithFrame:CGRectMake(200, 200, 100, 30)];
passwordSureT.layer.borderWidth = 1;
[self.view addSubview:passwordSureT];
// name
UILabel * nameL = [[UILabel alloc]initWithFrame:CGRectMake(100,250, 100, 30)];
nameL.text = @"真实姓名:";
[self.view addSubview: nameL];
nameT = [[UITextField alloc]initWithFrame:CGRectMake(200, 250, 100, 30)];
nameT.layer.borderWidth = 1;
[self.view addSubview:nameT];
// email
UILabel * emailL = [[UILabel alloc]initWithFrame:CGRectMake(100,300, 100, 30)];
emailL.text = @"电子邮箱:";
[self.view addSubview: emailL];
emailT = [[UITextField alloc]initWithFrame:CGRectMake(200, 300, 100, 30)];
emailT.layer.borderWidth = 1;
[self.view addSubview:emailT];
// 注册
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100 , 350, 100, 40);
button.backgroundColor = [UIColor blueColor];
[self.view addSubview:button];
[button setTitle:@"注册" forState:UIControlStateNormal];
[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
}
// 点击注册按钮
-(void)click
{
// get请求
NSString *urlString = [NSString stringWithFormat:@"http://127.0.0.1:8080/A/a?command=0&name=%@&psw=%@&truename=%@&email=%@",userT.text,passwordT.text,nameT.text,emailT.text];
NSLog(@"%@",urlString);
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *requst = [NSURLRequest requestWithURL:url];
[NSURLConnection connectionWithRequest:requst delegate:self];
NSLog(@"注册chenggong");
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"收到响应");
_data = [[NSMutableData alloc]init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"收到数据");
[_data appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"完成请求");
NSString *str = [[NSString alloc]initWithData:_data encoding:NSUTF8StringEncoding];
NSLog(@"完成请求后的内容:%@",str);
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"请求失败");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// 取消文本框编辑
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//
// LGViewController.m
// LoginDemo
//
// Created by xinguo on 15/10/12.
// Copyright (c) 2015年 2015-10-12. All rights reserved.
//
#import "LGViewController.h"
@interface LGViewController ()
@end
@implementation LGViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// label
UILabel * label = [[UILabel alloc]initWithFrame:self.view.frame];
label.text = @"登录成功";
[self.view addSubview:label];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
/*
get请求的特点 (当客户端要从服务器中读取文档时,当点击网页上的链接或者通过在浏览器的地址栏输入网址来浏览网页的,使用的都是get方式,它是最常见的一种请求数据的方式)
1.传输的长度有限制
2.不适合传输私密数据
3.不包含请求数据部分
post请求的特点(post方法将请求参数封装在http请求数据中,以名称/值的形式出现,可以传输大量数据,这样post方式对传送的数据大小没有限制,而且也不会显示在url中,大多用于页面的表单中)
1.理论上传输长度不受限制
2.传输数据相对安全
使用系统方法发送get网络请求
1
创建url字符串
2
创建url,将刚刚创建的字符创转变成URL
3
创建一个URLRequest对象,request的URL使用刚刚的URL
4
使用NSURLConnection方法
发送请求 使用上面的request
代理是自己
类似于
衣服放在箱包 人提着箱包,人开车走了。
衣服就是字符串、箱包就是URL、人就是Request、车就是NSURLConnection,开车的是自己。
post步骤
1根据URL创建一个请求对象
2参数拼接
3转码 把拼接好的字符串转化成data
4设置请求包体(往请求数据区添加数据)
5默认发get请求
6设置超时时间 秒
7发送请求(跟服务器建立一个连接) 发送同步请求(或异步请求)
8去掉首尾空格换行符
9判断登录 成功提示成功 失败提示错误信息
*/
// 下面是本人练习的一个登陆功能,利用服务器发送请求数据去注册和登陆用户
附上项目代码链接:http://yunpan.cn/cFKjQBUEImmNw (提取码:da8c)
@interface MainViewController : UIViewController<NSURLConnectionDataDelegate,NSURLConnectionDelegate>
{
UITextField *userName;
UITextField *password;
NSMutableData *_data;
}
@end
//
// MainViewController.m
// LoginDemo
//
// Created by xinguo on 15/10/12.
// Copyright (c) 2015年 2015-10-12. All rights reserved.
//
#import "MainViewController.h"
#import "ZCViewController.h"
#import "LGViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
self.title = @"登录界面";
// label
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 30)];
label.text = @"用户名:";
[self.view addSubview: label];
// label
UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 100, 30)];
label1.text = @"密码:";
[self.view addSubview: label1];
userName = [[UITextField alloc]initWithFrame:CGRectMake(200, 100, 100, 30)];
userName.layer.borderWidth = 1;
[self.view addSubview:userName];
password = [[UITextField alloc]initWithFrame:CGRectMake(200, 200, 100, 30)];
password.layer.borderWidth = 1;
[self.view addSubview:password];
// 注册
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100 , 300, 100, 40);
button.backgroundColor = [UIColor blueColor];
[self.view addSubview:button];
[button setTitle:@"注册" forState:UIControlStateNormal];
[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
// 登录
UIButton *login = [UIButton buttonWithType:UIButtonTypeCustom];
login.frame = CGRectMake(210 , 300, 100, 40);
login.backgroundColor = [UIColor blueColor];
[self.view addSubview:login];
[login setTitle:@"登录" forState:UIControlStateNormal];
[login addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
}
// 注册
-(void)click
{
ZCViewController *zcVC = [[ZCViewController alloc]init];
[self.navigationController pushViewController:zcVC animated:YES];
NSLog(@"zhuce");
}
// 登录
-(void)loginClick
{
/*
post步骤
1根据URL创建一个请求对象
2参数拼接
3转码 把拼接好的字符串转化成data
4设置请求包体(往请求数据区添加数据)
5默认发get请求
6设置超时时间 秒
7发送请求(跟服务器建立一个连接) 发送同步请求
8去掉首尾空格换行符
9判断登录 成功提示成功 失败提示错误信息
*/
NSString *urlString = @"http://127.0.0.1:8080/A/a";
// 1根据URL创建一个请求对象
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
// 2参数拼接
NSString *parameter = [NSString stringWithFormat:@"command=1&name=%@&psw=%@",userName.text,password.text];
NSLog(@"%@",parameter);
// 3转码 把拼接好的字符串转化成data
NSData *data = [parameter dataUsingEncoding:NSUTF8StringEncoding];
// 4设置请求包体(往请求数据区添加数据)
[request setHTTPBody:data];
// 5默认发get请求
[request setHTTPMethod:@"post"];
// 6设置超时时间 秒
[request setTimeoutInterval:20];
// NSLog(@"%@",request);
// 7发送请求(跟服务器建立一个连接) 发送同步请求
NSURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *str =[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
// 8去掉首尾空格换行符
NSString *newStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
// 判断登录 成功提示成功 失败提示错误信息
if ([newStr isEqualToString:@"登录成功!"])
{
LGViewController *lg = [[LGViewController alloc]init];
[self.navigationController pushViewController:lg animated:YES];
NSLog(@"lgoin");
}else
{
// 提示出错
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"错误提示" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
[alertView show] ;
}
}
//收到响应,意味着数据马上就要来了
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)respons
{
NSLog(@"收到响应");
_data = [[NSMutableData alloc]init];
}
//收到数据,这个方法会被调用多次,因为数据量太大的话,服务器不会一次性把数据全给你
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"收到数据");
[_data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"请求完成");
//把data转化成字符串
// NSString *str = [[NSString alloc]initWithData:_data encoding:NSUTF8StringEncoding];
// NSLog(@"%@",str);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"请求失败");
}
// 取消文本框编辑
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//
// AppDelegate.m
// LoginDemo
//
// Created by xinguo on 15/10/12.
// Copyright (c) 2015年 2015-10-12. All rights reserved.
//
#import "AppDelegate.h"
#import "MainViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self.window makeKeyAndVisible];
MainViewController *mainVC = [[MainViewController alloc]init];
UINavigationController *mainNC = [[UINavigationController alloc]initWithRootViewController:mainVC];
self.window.rootViewController = mainNC;
// Override point for customization after application launch.
return YES;
}
//
// ZCViewController.h
// LoginDemo
//
// Created by xinguo on 15/10/12.
// Copyright (c) 2015年 2015-10-12. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ZCViewController : UIViewController<NSURLConnectionDataDelegate,NSURLConnectionDelegate>
{
UITextField *userT;
UITextField *passwordT;
UITextField *passwordSureT;
UITextField *nameT;
UITextField *emailT;
NSMutableData *_data;
}
@end
//
// ZCViewController.m
// LoginDemo
//
// Created by xinguo on 15/10/12.
// Copyright (c) 2015年 2015-10-12. All rights reserved.
//
#import "ZCViewController.h"
@interface ZCViewController ()
@end
@implementation ZCViewController
// 加载
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// userL
UILabel * userL = [[UILabel alloc]initWithFrame:CGRectMake(100,100, 100, 30)];
userL.text = @"用户名:";
[self.view addSubview: userL];
userT = [[UITextField alloc]initWithFrame:CGRectMake(200, 100, 100, 30)];
userT.layer.borderWidth = 1;
[self.view addSubview:userT];
// password
UILabel * passwordL = [[UILabel alloc]initWithFrame:CGRectMake(100,150, 100, 30)];
passwordL.text = @"密码:";
[self.view addSubview: passwordL];
passwordT = [[UITextField alloc]initWithFrame:CGRectMake(200, 150, 100, 30)];
passwordT.layer.borderWidth = 1;
[self.view addSubview:passwordT];
// passwordSure
UILabel * passwordSureL = [[UILabel alloc]initWithFrame:CGRectMake(100,200, 100, 30)];
passwordSureL.text = @"密码确认:";
[self.view addSubview: passwordSureL];
passwordSureT = [[UITextField alloc]initWithFrame:CGRectMake(200, 200, 100, 30)];
passwordSureT.layer.borderWidth = 1;
[self.view addSubview:passwordSureT];
// name
UILabel * nameL = [[UILabel alloc]initWithFrame:CGRectMake(100,250, 100, 30)];
nameL.text = @"真实姓名:";
[self.view addSubview: nameL];
nameT = [[UITextField alloc]initWithFrame:CGRectMake(200, 250, 100, 30)];
nameT.layer.borderWidth = 1;
[self.view addSubview:nameT];
UILabel * emailL = [[UILabel alloc]initWithFrame:CGRectMake(100,300, 100, 30)];
emailL.text = @"电子邮箱:";
[self.view addSubview: emailL];
emailT = [[UITextField alloc]initWithFrame:CGRectMake(200, 300, 100, 30)];
emailT.layer.borderWidth = 1;
[self.view addSubview:emailT];
// 注册
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100 , 350, 100, 40);
button.backgroundColor = [UIColor blueColor];
[self.view addSubview:button];
[button setTitle:@"注册" forState:UIControlStateNormal];
[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
}
// 点击注册按钮
-(void)click
{
// get请求
NSString *urlString = [NSString stringWithFormat:@"http://127.0.0.1:8080/A/a?command=0&name=%@&psw=%@&truename=%@&email=%@",userT.text,passwordT.text,nameT.text,emailT.text];
NSLog(@"%@",urlString);
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *requst = [NSURLRequest requestWithURL:url];
[NSURLConnection connectionWithRequest:requst delegate:self];
NSLog(@"注册chenggong");
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"收到响应");
_data = [[NSMutableData alloc]init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"收到数据");
[_data appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"完成请求");
NSString *str = [[NSString alloc]initWithData:_data encoding:NSUTF8StringEncoding];
NSLog(@"完成请求后的内容:%@",str);
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"请求失败");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// 取消文本框编辑
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//
// LGViewController.m
// LoginDemo
//
// Created by xinguo on 15/10/12.
// Copyright (c) 2015年 2015-10-12. All rights reserved.
//
#import "LGViewController.h"
@interface LGViewController ()
@end
@implementation LGViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// label
UILabel * label = [[UILabel alloc]initWithFrame:self.view.frame];
label.text = @"登录成功";
[self.view addSubview:label];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
相关文章推荐
- 7层网络模型
- iOS开发swift版异步加载网络图片(带缓存和缺省图片)
- 对网络协议的理解
- BitmapFactory.decodeStream方法返回null的错误分析
- TCP和UDP
- Android开发菜鸟级入门4--网络编程之套接字UDP
- Ubuntu设置自动连接无线网络
- http://www.cnblogs.com/bluestorm/archive/2012/05/10/2493592.html
- SNS用户关系网络合并
- Linux下网络编程TCP/UDP简介
- http
- Theano入门——卷积神经网络
- Theano入门——神经网络
- 动手学习TCP:4种定时器
- Android中Parcelable接口用法http://www.cnblogs.com/renqingping/archive/2012/10/25/Parcelable.html
- 配置linux httpd conf
- 洛谷1262间谍网络(tarjan缩点)
- HTTP、Scoket网络协议浅解
- Java基础知识强化之网络编程笔记14:TCP之多个客户端上传到一个服务器的思考(多线程改进)
- UDP&TCP协议