iOS编程--------网络编程 iOS平台如何实现HTTP协议请求

//
//  AppDelegate.h
//  UI16_networkingProgramming
//
//  Created by l on 15/9/22.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end






//
//  AppDelegate.m
//  UI16_networkingProgramming
//
//  Created by l on 15/9/22.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //UI16 网络编程
    //主要知识点:
    //1. C/S 客户端服务器模式
    //2. url 统一资源定位符的理解
    //3. 完整的网络请求
    //4. 异步网络请求,  同步网络请求
    //5. 网络数据解析

    // 三. 完整的网络请求
    //1. 网络请求地址url NSURL
    NSURL *getURL = [[NSURL alloc] initWithString:@"http://bea.wufazhuce.com/OneForWeb/one/getC_N?strDate=2015-09-22&strRow=1&strMS=1"];

    //2.网络请求对象NSURLRequest  NSMutableURLRequest
    //网络请求对象 作用是 使用网络地址发起网络请求
    //在iOS中网络请求对象的请求方式分为get请求方式和post请求方式. get请求方式url中包含查询参数(?参数)  如: ?strDate=2015-09-22&strRow=1&strMS=1
    //post 请求方式url中不包含请求参数,而是把参数放到了httpBody里面
    //NSURLRequest 不可变网络请求默认为get请求,不可以设置为post请求
    //NSMutableURLRequest可变网络请求方式,默认为get请求,可以通过设置httpMethod:可变post请求

    //2.建立Request对象
    NSURLRequest *requestGet = [[NSURLRequest alloc] initWithURL:getURL cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:60];



    /*
     //网络缓存:
    NSURLCache *urlCache = [NSURLCache sharedURLCache];
    // 2M

    [urlCache setMemoryCapacity:2 * 1024 * 1024];

    // 缓存响应
    NSCachedURLResponse *response = [urlCache cachedResponseForRequest:requestGet];

    // 判断是否有本地缓存
    if (response) {
        // 没有网络的情况如果 缓存中有数据,读取缓存数据
        NSLog(@"没有网络,读取缓存数据");
        [requestGet setCachePolicy:(NSURLRequestReturnCacheDataDontLoad)];
    }

    */


    //3.connection NSURLConnection创建的
    //网络连接方式:有同步方式和异步方式两种
    //1.同步方式,会卡主线程,等到网络请求结束后,主线程才会接着往下执行
    //2.异步方式,与主线程并行执行,互不干预,不会卡主线程,单数数据请求完毕后需要返回主线程

    //异步:Asynchronous
    [NSURLConnection sendAsynchronousRequest:requestGet queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        //数据解析完毕后返回主线程,block回调到主线程中的时候,数据已经解析完毕,block的参数data,即为解析后的data数据
//        NSLog(@"%@", data);

        //4.网络解析 json解析
        //(1) 取到根节点
        NSDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

        // contentEntity对应的字典
        NSDictionary *contentDic = [rootDic objectForKey:@"contentEntity"];
        //strContent
        NSString *contentStr = [contentDic objectForKey:@"strContent"];

//        NSLog(@"%@", contentStr);

    }];




    //----------------------------//

    //post 网络请求
    //post 网络请求的url中不包含请求参数,而是放到了httpBody里面
    //http://bea.wufazhuce.com/OneForWeb/one/getC_N?strDate=2015-09-22&strRow=1&strMS=1

   /* //1.post网络请求地址
    NSURL *postURL = [[NSURL alloc] initWithString:@"http://bea.wufazhuce.com/OneForWeb/one/getC_N"];

    //2.网络请求对象
    NSMutableURLRequest *requestPost = [[NSMutableURLRequest alloc] initWithURL:postURL cachePolicy:0 timeoutInterval:10];

    //(1) 设置httpMethod请求方式
    [requestPost setHTTPMethod:@"POST"];
    //(2)设置httpBody请求体
    //请求体为url中的查询参数
    [requestPost setHTTPBody:[@"strDate=2015-09-22&strRow=1&strMS=1" dataUsingEncoding:(NSUTF8StringEncoding)]];

    //3.网络连接对象
    //Sync 同步网络请求,会卡线程
    //当程序执行到这行代码时,会一直执行网络请求,直到网络请求结束,才会继续执行.
    NSData *data = [NSURLConnection sendSynchronousRequest:requestPost returningResponse:nil error:nil];

    NSLog(@"%@", data);



    //4.网络解析 json解析
    //(1) 取到根节点
    NSDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

    // contentEntity对应的字典
    NSDictionary *contentDic = [rootDic objectForKey:@"contentEntity"];
    //strContent
    NSString *contentStr = [contentDic objectForKey:@"strContent"];

    NSLog(@"%@", contentStr);
*/

    //----------------------//

    //总结:完整的网络请求
    //1.网络地址 url 根据请求方式不同,需要设置不同的url,带参数和不带参数的
    //2.网络请求对象 Request
    //可以分为get请求方式,明文传输,不安全,最大传输为255个字节; post请求方式,data数据,安全,传输最大为1g
    //get 请求对象 NSURLRequest  url带参数
    //post 请求对象 NSMutableRequest url不带参数,需要设置, httpMothod:@"POST", httpBody:[@"参数" data转换]

    //3.网络连接 connection
    //分为:同步网络连接请求 异步网络连接请求
    //同步:sendSync
    //异步: 1.sendAsync 2.代理方式

    //iOS 中按照请求方式不同 以及 请求连接同异步可以分为4种
    //get 同步 异步
    //post 同步 异步

    //我们今后使用get + 代理异步请求或者get + block 异步请求 (sendAscyn)





    // Override point for customization after application launch.
    return YES;
}

@end






///





//
//  ViewController.h
//  UI16_networkingProgramming
//
//  Created by l on 15/9/22.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end







//
//  ViewController.m
//  UI16_networkingProgramming
//
//  Created by l on 15/9/22.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<NSURLConnectionDelegate,NSURLConnectionDataDelegate>
//接收传输过来的数据
@property (nonatomic, retain) NSMutableData *receivedata;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //异步连接代理方式
    //1.url
    NSURL *url = [[NSURL alloc] initWithString:@"http://bea.wufazhuce.com/OneForWeb/one/getC_N?strDate=2015-09-22&strRow=1&strMS=1"];
    //2.request
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:0 timeoutInterval:10];

    //3.connection 给连接设置代理
//       [[NSURLConnection alloc] initWithRequest:request delegate:self];

    [NSURLConnection connectionWithRequest:request delegate:self];

    // Do any additional setup after loading the view, typically from a nib.
}

//-------------//

//接收到网络回应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    NSLog(@"%@", response);

    if (response) {
        //如果有网络回应,给receiveData进行初始化
        self.receivedata = [NSMutableData data];
    }

}

//接收到数据的时候触发
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

    [self.receivedata appendData:data];
}

//数据接收完成的时候触发
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

    //我们在数据接收完成后进行数据解析
    NSDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:self.receivedata options:0 error:nil];

    // contentEntity对应的字典
    NSDictionary *contentDic = [rootDic objectForKey:@"contentEntity"];
    //strContent
    NSString *contentStr = [contentDic objectForKey:@"strContent"];

    NSLog(@"%@", contentStr);

}

//当连接失败的时候执行
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

    NSLog(@"%s %d %@", __FUNCTION__, __LINE__, error);

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值