#import "XMLDictionary.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
那么这些误区是怎么出现的呢?先看看一下对http的解释
一般在浏览器中输入网址访问资源都是通过GET方式;在FORM提交中,可以通过Method指定提交方式为GET或者POST,默认为GET提交
Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE
URL全称是资源描述符,我们可以这样认为:一个URL地址,它用于描述一个网络上的资源,而HTTP中的GET,POST,PUT,DELETE就对应着对这个资源的查 ,改 ,增 ,删 4个操作。到这里,大家应该有个大概的了解了,GET一般用于获取/查询 资源信息,而POST一般用于更新 资源信息(个人认为这是GET和POST的本质区别,也是协议设计者的本意,其它区别都是具体表现形式的差异 )。
再进一步了解下他们两个的区别:
1. GET使用URL或Cookie传参。而POST将数据放在BODY中。
2. GET的URL会有长度上的限制,则POST的数据则可以非常大。
3. POST比GET安全,因为数据在地址栏上不可见。
NSString *path=@" http://1.studyios.sinaapp.com/gyxy.php?a=qq";
// 创建请求对象
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:path]];
// 1.创建一个会话对象
NSURLSession *session=[NSURLSession sharedSession];
// 2.通过请求信息,获取数据结构(根据会话创建一个task 发送请求)。
// completionHandler 回调 data响应体信息 response:响应头信息,主要是对服务器端的描述error:错误信息,如果请求失败,则error有值
NSURLSessionDataTask *task=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSArray *arr=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
NSLog(@"%@",arr);
}];
// 执行任务
[task resume];
@interface ViewController : UIViewController<UITextFieldDelegate>
@property(strong,nonatomic) UITextField *txtInfo;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.txtInfo=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 150, 44)];
self.txtInfo.borderStyle=1;
self.txtInfo.delegate=self;
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSString *path=[NSString stringWithFormat:@"http://1.studyios.sinaapp.com/gyxy.php?a=%@&b=bbb&c=ccc",self.txtInfo.text]
;
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:path]];
// 1.创建一个会话对象
NSURLSession *session=[NSURLSession sharedSession];
// 2.通过请求信息 获取数据结构
NSURLSessionDataTask *task=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// NSLog(@"%@",data);
NSArray *arr= [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
NSLog(@"%@",arr);
}];
[task resume];
return YES;
使用NSURLSession发送POST请求的方法和NSURLConnection类似,整个过程如下:
1)确定请求路径(一般由公司的后台开发人员以接口文档的方式提供)
2)创建 可变的 请求对象(因为需要修改),此步骤不可以省略
3)修改请求方法为POST
4)设置请求体,把参数转换为二进制数据并设置请求体
5)创建会话对象(NSURLSession)
6)根据会话对象创建请求任务(NSURLSessionDataTask)
// day16-2
//
// Created by tlh on 16/3/28.
// Copyright © 2016年 lamco. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path=@" http://1.studyios.sinaapp.com/mypost.php";
NSURL *url=[NSURL URLWithString:path];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
// post请求
request.HTTPMethod=@"POST";
// 参数
NSString *param=[NSString stringWithFormat:@"can1=test&can2=test1"];
// 将参数转换成data类型数据
NSData *dataparam=[param dataUsingEncoding:NSUTF8StringEncoding];
// request的参数的主体
request.HTTPBody=dataparam;
NSURLSession *session=[NSURLSession sharedSession];
NSURLSessionDataTask *task=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error]);
}];
[task resume];