美文网首页
GET、POST请求

GET、POST请求

作者: MI移动 | 来源:发表于2017-07-20 11:17 被阅读0次

GET请求

#import "RootViewController.h"

@interface RootViewController ()<NSURLConnectionDataDelegate>

@property(nonatomic,strong)UIButton *getButton;

// 声明一个属性,用来接受请求下来的data
@property(nonatomic,strong)NSMutableData *myData;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.getButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.getButton.frame = CGRectMake(100, 100, 100, 100);
    [self.getButton setTitle:@"get请求" forState:UIControlStateNormal];
    [self.view addSubview:self.getButton];
    [self.getButton addTarget:self action:@selector(getButtonAction:) forControlEvents:UIControlEventTouchUpInside];
   
    // Do any additional setup after loading the view.
}
- (void)getButtonAction:(UIButton *)sender{
    // GET请求 同步
//    [self p_getData];
    // GET请求 异步使用代理
//    [self p_getData2];
    // GET请求 异步使用Block
    [self p_getData3];
}

- (void)p_getData{
    // 1. 准备URL
    NSString *urlStr = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchcinema.php?cinemaId=141"];
    NSURL *url = [NSURL URLWithString:urlStr];
    // 2. 准备请求
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
    // 设置请求方法
    [request setHTTPMethod:@"GET"];
   
    // 3. 准备数据
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
   
    // 4. 解析
    NSError *error = nil;
   
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
   
    NSLog(@"%@",dict);
   
}
- (void)p_getData2{
    // 1 准备URL
    NSString *urlStr = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchcinema.php?cinemaId=141"];
    NSURL *url = [NSURL URLWithString:urlStr];
   
    // 2. 准备请求
    NSMutableURLRequest *reguest = [[NSMutableURLRequest alloc]initWithURL:url];
   
    // 3. 准备连接(使用代理方法 实现异步请求)
    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:reguest delegate:self];
   
//    // 4. 开始连接
//    [connection start];
//    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:reguest delegate:self startImmediately:YES];

}
- (void)p_getData3{
    // 1. 准备URL
    NSString *urlStr = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchcinema.php?cinemaId=141"];
    NSURL *url = [NSURL URLWithString:urlStr];
   
    // 2. 准备请求
    NSMutableURLRequest  *reguest = [[NSMutableURLRequest alloc]initWithURL:url];
   
    // 3. 使用Block异步解析
    [NSURLConnection sendAsynchronousRequest:reguest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
      
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@",dict);
    }]; 
}

#pragma mark GET第二种异步代理方法
// 1. 接受到响应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    self.myData = [NSMutableData data];
}

// 2. 接收到数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [self.myData appendData:data];
}

// 3. 已经完成加载
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:self.myData options:NSJSONReadingAllowFragments error:nil];
    NSLog(@"%@",dict);
}

POST请求 (可以设置参数)

#import "RootViewController.h"

@interface RootViewController ()<NSURLConnectionDataDelegate>

@property(nonatomic,strong)UIButton *postButton;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.postButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.postButton.frame = CGRectMake(100, 100, 100, 100);
    [self.postButton setTitle:@"post请求" forState:UIControlStateNormal];
    [self.view addSubview:self.postButton];
   
    [self.postButton addTarget:self action:@selector(postButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    // Do any additional setup after loading the view.
}

- (void)postButtonAction:(UIButton *)sender{
    // post 同步
 //   [self p_postData];
    // post 异步
    [self p_postData1];
}

- (void)p_postData{
    NSLog(@"Post请求(同步)");
   
    NSString *urlStr = [NSString stringWithFormat:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];
   
    NSURL *url = [NSURL URLWithString:urlStr];
   
    NSMutableURLRequest *reguest = [[NSMutableURLRequest alloc]initWithURL:url];
   
    [reguest setHTTPMethod:@"POST"];
   
    // 准备参数
    NSString *argument = [NSString stringWithFormat:@"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"];
    NSData *argumentData = [argument dataUsingEncoding:NSUTF8StringEncoding];
    // 设置URL的参数
    [reguest setHTTPBody:argumentData];
   
   
   NSData *data =  [NSURLConnection sendSynchronousRequest:reguest returningResponse:nil error:nil];
   
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
   
    NSLog(@"%@",dict);
}
- (void)p_postData1{
    // 1. 准备URL
    NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];
   
    // 2. 准备请求
    NSMutableURLRequest  *request = [[NSMutableURLRequest alloc ]initWithURL:url];
   
    [request setHTTPMethod:@"POST"];
   
    // 3. 设置参数
    NSString *argument = [NSString stringWithFormat:@"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"];
   
    // 将字符串参数装换成NSData
    NSData *argData = [argument dataUsingEncoding:NSUTF8StringEncoding];
   
    // 将参数添加到URL
    [request setHTTPBody:argData];
   
    // 准备连接
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
   
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
       
        NSLog(@"%@",dict);
    }];
}

相关文章

  • iOS请求方法和网络安全

    GET和POST请求 GET和POST请求简介 GET请求模拟登陆 POST请求模拟登陆 GET和POST的对比 ...

  • iOS请求方法和网络安全

    GET和POST请求GET和POST请求简介GET请求模拟登陆POST请求模拟登陆GET和POST的对比保存用户信...

  • java发送http请求

    restTemplate get请求 post请求 apache.http.client get请求 post请求...

  • Get和Post的区别

    Get请求和Post请求区别如下: Post请求比Get请求更安全,get请求直接将参数放置在URL中,post请...

  • Okhttp3

    简介 配置 请求思路 get请求思路 post请求思路 get,post 同步和异步请求 异步请求(get) 同步...

  • gf框架 ghttp使用

    案例中包含以下内容 get请求 get请求携带参数 post请求携带参数 post请求发送xml数据 post请求...

  • HttpUtil工具

    HttpUtil工具,http get post请求,https get post请求,ajax response...

  • ajax 请求的时候 get 和 post 方式的区别?

    get和post的区别 get请求不安全,post安全 get请求数据有限制,post无限制 get请求参数会在u...

  • get和post请求区别

    get请求和post请求 差别 get请求回退时无反应,post请求回退时会再次发起请求。 GET请求只能进行ur...

  • iOS 的网络请求案例

    post请求: get请求:

网友评论

      本文标题:GET、POST请求

      本文链接:https://www.haomeiwen.com/subject/owedkxtx.html