urlurl

来源:互联网 发布:ubuntu怎么使用putty 编辑:程序博客网 时间:2024/06/07 16:36

伤心总是难免的,何苦一往情深,总之爱情难舍难分,何必在意那一点点温存,早知道伤心总是难免的,何苦一往情深,,有的人你永远不会懂

#import "ViewController.h"#pragma mark - 重定义网址#define BASE_URL_1 @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"#define BASE_URL_2 @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?"#define BASE_URL_2_PARAM @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"#pragma mark - 添加异步链接的代理--异步需代理实现@interface ViewController ()<NSURLConnectionDelegate,NSURLConnectionDataDelegate>@property (weak, nonatomic) IBOutlet UITextView *textView;@property(nonatomic, strong) NSMutableData *allData;   //声明一个属性,异步代理方法执行@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.}#pragma mark - get同步- (IBAction)getSyncAction:(UIButton *)sender {    //1.网络地址    NSURL *url = [NSURL URLWithString:BASE_URL_1];    //2.通过网址创建请求对象    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];    //2.1.设置请求方式    [request setHTTPMethod:@"GET"];        //3创建连接对象,发送请求,获取数据    //3.1获取返回信息的对象    NSURLResponse *response = [NSURLResponse new];    //3.2保存错误信息的对象    NSError *error = nil;    //3.3发送请求    /**     *  request:发送的请求        response:接受结果的信息        error:保存发生错误时候的错误信息     */   NSData *resdata = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];    //接收到的结果都是二进制        //4.解析---把数据解析为dict    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:resdata options:NSJSONReadingAllowFragments error:nil];                //下面是练习//    NSURL *url = [NSURL URLWithString:BASE_URL_1];//    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];//    [request setHTTPMethod:@"GET"];//    NSURLResponse *response = [NSURLResponse new];//    //    NSData *resdata = [NSURLConnection sendSynchronousRequest:request  returningResponse:&response error:nil];//    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:resdata options:NSJSONReadingAllowFragments error:nil];                        }#pragma mark - get异步 --- 需要5个方法- (IBAction)getAsyncAction:(UIButton *)sender {        //1.网络地址    NSURL *url = [NSURL URLWithString:BASE_URL_1];    //2.通过网址创建请求对象    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];    //2.1.设置请求方式    [request setHTTPMethod:@"GET"];        //每个步骤 都要用代理方法走        //3.使用异步链接,发送请求    [NSURLConnection connectionWithRequest:request delegate:self];                }#pragma mark - post同步method- (IBAction)postSyncAction:(UIButton *)sender {    //1.准备网址    NSURL *url = [NSURL URLWithString:BASE_URL_2];    //2.请求对象    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];    [request setHTTPMethod:@"POST"];   //设置请求方式        //将字符串转化为data类型的数据    NSData *paramData = [BASE_URL_2_PARAM dataUsingEncoding:NSUTF8StringEncoding];    //设置请求参数(data类型)    [request setHTTPBody:paramData];        //3.创建链接对象,发送请求,获取连接数据    NSData *resData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];    NSLog(@"%@", resData);        //4.解析                }#pragma mark - POST异步- (IBAction)postAyncAction:(UIButton *)sender {        //1.准备网址    NSURL *url = [NSURL URLWithString:BASE_URL_2];    //2.请求对象    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];    [request setHTTPMethod:@"POST"];   //设置请求方式        //将字符串转化为data类型的数据    NSData *paramData = [BASE_URL_2_PARAM dataUsingEncoding:NSUTF8StringEncoding];    //设置请求参数(data类型)    [request setHTTPBody:paramData];            //3.使用连接,发送请求,获取数据    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {        NSLog(@"%@", data);                //解析        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];    }];    }- (IBAction)cleatAction:(UIButton *)sender {}#pragma mark - 异步代里方法 --#pragma mark - 已经接受响应 - 执行1次- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{    //1                                             
0 0
原创粉丝点击