ios网络编程01

在iOS下进行网络编程主要分为以下三步:
1.客户端向服务器发送请求
客户端和服务器请求的方式主要有两种:GET和POST
GET请求:将信息直接拼接在URL的后面。(在真正开发中用到的比较多)
POST请求:将一些比较重要的信息转化成二进制流。(在真正开发中,POST请求常常用于验证用户名,密码是否是否正确,给服务器上传数据等)
注意:1.不管是GET请求还是POST请求都是有后台决定的,程序员可以通过接口文档知道当前的请求方式。
2.请求方式还有delete,put等方式。除了GET请求可以使用NSURLRequest之外,其他的请求必须使用NSURLMutableRequest,明确指定当前的HTTPMethod是什么请求。
2.和服务器建立连接
3.服务器做出响应

    NSURL *url = [NSURL URLWithString:@"www.baidu.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSError *error = nil;
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:&error];



   }];//GET方式
   NSURL *url1 = [NSURL URLWithString:@"hahahaha"];

   NSMutableURLRequest *request1 = [NSMutableURLRequest requestWithURL:url1 cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15];
    [request1 setHTTPMethod:@"POST"];
    NSData *date = [@"dashdka" dataUsingEncoding: NSUTF8StringEncoding];
    [request1 setHTTPBody:date];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

    }];//POST方式
    NSURL *urll = [NSURL URLWithString:@"http://api.tudou.com/v3/gw?method=album.channel.get&appKey=myKey&format=xml&channel=c&pageNo=1&pageSize=15"];
    NSString *contentStr = [NSString stringWithContentsOfURL:urll encoding:NSUTF8StringEncoding error:nil];
    // 3.转化为XMLDocument(当执行完这一步时,整个文档已经被解析完毕,并且转化为XMLDocument进行存储。同时文档对应的树形结构也已经清晰明了了)
    GDataXMLDocument *document = [[GDataXMLDocument alloc]initWithXMLString:contentStr options:0 error:nil];

    NSArray *node = [document nodesForXPath:@"// /picUrl" error:nil];
    NSLog(@"%@",node[0]);
    UIImageView *aaa =[[UIImageView alloc]initWithImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[node[0] stringValue]]]]];//XML解析
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值