现在的位置: 首页 > 综合 > 正文

ios-GET和POST

2016年08月30日 ⁄ 综合 ⁄ 共 2600字 ⁄ 字号 评论关闭
// HTTP协议
/*
 1、HTTP是Hypertext Transfer Protocol的缩写,即超文本传输协议。网络中使用的基本协议是TCP/UDP协议,目前广泛采用的HTTP、HTTPS、FTP、Archie和Gropher等都是建立在TCP/UDP协议之上的应用层协议。
 2、HTTP协议支持C/S网络结构,是无连接协议,即每一次请求时建立连接,服务器处理完客户端的请求之后,应答给客户端然后断开连接,不会一直占用网络资源。
 3、HTTP协议共定义了8种请求方法:OPTIONS、HEAD、GET、POST、PUT、DELETE、TRACE、和CONNECT。作为Web服务器,必须实现GET和HEAD方法,其它方法都是可选的。
 */

// HTTPS协议
/*
 1、HTTPS是Hypertext Transfer Protocol Secure的缩写,即超文本传输安全协议,是超文本传输协议和SSL的组合,用以提供加密通信及对网络服务器身份的鉴定。
 2、HTTPS使用https://,而HTTP使用http://;HTTPS使用端口443,而HTTP使用80.
 */

// HTTP/HTTPS中的GET和POST方法
/*
 1、GET方法是向指定的资源发出请求,发送的消息“显式”地跟在URL后面。GET方法应该只用在读取数据,例如静态图片等。GET方法有点像使用明信片给别人信写信,“信内容”写在外面,接触到的人都可以看到,因此是不安全的。
 2、POST方法是向指定的资源提交数据,请求服务器进行处理,例如提交表单或者上传文件等。数据被包含在请求体中。POST方法像是把“信内容”装入信封中,接触到的人是看不到的,因此是安全的。
 */


// 同步GET(Synchro GET)
- (void)SynchroGet:(id)sender
{
    NSString *str = [NSString stringWithFormat:@"http://iosbook1.com/service/mynotes/webservice.php?email=%@&type=%@&action=%@",@"chenximincool@163.com",@"JSON",@"query"];//数据显示在链接中:email=%@&type=%@&action=%@",@"chenximincool@163.com",@"JSON",@"query
    
    NSURL *url = [NSURL URLWithString:str];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];  // 同步请求
    
    NSLog(@"请求完成!--data:%@",data);
}

// 异步GET(Asynchro GET)
- (void)AsynchroGet:(id)sender
{
    NSString *str = [NSString stringWithFormat:@"http://iosbook1.com/service/mynotes/webservice.php?email=%@&type=%@&action=%@",@"chenximincool@163.com",@"JSON",@"query"];
    
    NSURL *url = [NSURL URLWithString:str];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    NSURLConnection *connect = [[NSURLConnection alloc] initWithRequest:request delegate:self];  // 异步请求
}

// 异步POST(Asynchro POST)
- (void)AsynchroPOST:(id)sender
{
    NSString *str = [NSString stringWithFormat:@"http://iosbook1.com/service/mynotes/webservice.php"];
    
    NSString *post = [NSString stringWithFormat:@"email=%@&type=%@&action=%@",@"chenximincool@163.com",@"JSON",@"query"];
    NSData *data = [post dataUsingEncoding:NSUTF8StringEncoding];
    
    NSURL *url = [NSURL URLWithString:str];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:data];
    
    NSURLConnection *connect = [[NSURLConnection alloc] initWithRequest:request delegate:self];  // 异步请求
}

// 异步代理
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"didReceiveData--%@",data);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError--%@",[error localizedDescription]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"connectionDidFinishLoading--请求完成!");
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

抱歉!评论已关闭.