以下是代码,凝视也写得比較清楚:

头文件须要实现协议NSURLConnectionDelegate和NSURLConnectionDataDelegate

//
// HttpDemo.h
// MyAddressBook
//
// Created by hherima on 14-6-23.
// Copyright (c) 2014年. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> @interface HttpDemo : NSObject<NSURLConnectionDelegate, NSURLConnectionDataDelegate>
{
NSMutableData *receivedData;
NSURLConnection *theConncetion;
}
@end

源文件

//
// HttpDemo.m
// MyAddressBook
//
// Created by hherima on 14-6-23.
// Copyright (c) 2014年. All rights reserved.
// #import "HttpDemo.h"
@implementation HttpDemo
/*
NSURLConnection 提供了非常多灵活的方法下载URL内容,也提供了一个简单的接口去创建和放弃连接,同一时候使用非常多的delegate方法去支持连接过程的反馈和控制 举例:
1、先创建一个NSURL
2、再通过NSURL创建NSURLRequest,能够指定缓存规则和超时时间
3、创建NSURLConnection实例,指定NSURLRequest和一个delegate对象
假设创建失败,则会返回nil,假设创建成功则创建一个NSMutalbeData的实例用来存储数据
*/
- (id)init {
self = [super init];
// Override point for customization after application launch.
NSURLRequest* theRequest = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"http://www.baidu.com"]//
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60.0];
//当收到initWithRequest: delegate: 消息时,下载会马上開始,在代理(delegate)
//收到connectionDidFinishLoading:或者connection:didFailWithError:消息之前
//能够通过给连接发送一个cancel:消息来中断下载
theConncetion=[[NSURLConnection alloc]
initWithRequest:theRequest delegate:self];
if(theConncetion)
{
//创建NSMutableData
receivedData = [NSMutableData data];
}
else
{
//创建失败;
} return self;
} //当server提供了足够客户程序创建NSURLResponse对象的信息时。代理对象会收到一个connection:didReceiveResponse:消息。在消息内能够检查NSURLResponse对象和确定数据的预期长途,mime类型。文件名称以及其它server提供的元信息 //【要注意】,一个简单的连接也可能会收到多个connection:didReceiveResponse:消息当server连接重置或者一些罕见的原因(比方多组mime文档)。代理都会收到该消息这时候应该重置进度指示,丢弃之前接收的数据
-(void)connection:(NSURLConnection *)connectiondidReceiveResponse:(NSURLResponse*)response
{
[receivedData setLength:0];
} //当下载開始的时候,每当有数据接收,代理会定期收到connection:didReceiveData:消息代理应当在实现中储存新接收的数据,以下的样例既是如此
-(void) connection:(NSURLConnection*)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
} //当代理接收到连接的connection:didFailWithError消息后,对于该连接不会在收到不论什么消息
-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
theConncetion = nil; NSLog(@"Connection failed! Error - %@ %@",[error localizedDescription],[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); }
//数据完成下载,最后,假设连接请求成功的下载,代理会接收connectionDidFinishLoading:消息代理不会收到其它的消息了,在消息的实现中。应该释放掉连接
-(void)connectionDidFinishLoading:(NSURLConnection*)connection
{
//do something with the data
NSString *s = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"succeeded %@",s);
theConncetion = nil;
[receivedData setLength:0];
}
@end

简单使用NSURLConnection、NSURLRequest和NSURL的更多相关文章

  1. OC - 12.NSURLRequest与NSURLConnection

    ##NSURLRequest NSURLRequest封装了一次网络请求所需要的数据,主要封装了以下信息: 请求路径(URL) 请求方法(GET或POST) 请求头 请求体 超时参数 NSURLReq ...

  2. iOS开发——网络篇——NSURLSession,下载、上传代理方法,利用NSURLSession断点下载,AFN基本使用,网络检测,NSURLConnection补充

    一.NSURLConnection补充 前面提到的NSURLConnection有些知识点需要补充 NSURLConnectionDataDelegate的代理方法有一下几个 - (void)conn ...

  3. NSURLConnection、NSURLSession

    NSURLConnection   1.准备网络资源地址:URL 注意:由于URL支持26个英文字母,数字和少数的几个特殊字符. 因此对于URL中包含非标准URL的字符,需要进行编码. iOS提供了函 ...

  4. iOS 网络编程:NSURLConnection

    1 简介 1.1 概念 NSURLConnection类似NSURLSession,都是进行网络数据传输的.其中NSURLSession是NSURLConnection的替代版本,目前IOS9.0几乎 ...

  5. iOS网络通信http之NSURLConnection

    iOS网络通信http之NSURLConnection 移动互联网时代,网络通信已是手机终端必不可少的功能.我们的应用中也必不可少的使用了网络通信,增强客户端与服务器交互.这一篇提供了使用NSURLC ...

  6. 教你如何封装异步网络连接NSURLConnection实现带有百分比的下载

    教你如何封装异步网络连接NSURLConnection实现带有百分比的下载 注:本教程需要你对block有着较为深刻的理解,且对如何封装对象有着一些经验. 也许你已经用惯了AFNetworking2. ...

  7. WebView loadRequest请求错误"NSURLConnection finished with error - code -1022"

    执行下面代码 [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www ...

  8. post NSURLConnection请求网络数据

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  9. iOS网络-01-NSURLRequest与NSURLConnection

    NSURLRequest NSURLRequest封装了一次网络请求所需要的数据,主要封装了以下信息: 请求路径(URL) 请求方法(GET或POST) 请求头 请求体 超时参数 NSURLReque ...

随机推荐

  1. Spring和CXF整合时报Unsupported major.minor version 51.0异常

    好吧,官网上有写:The current plan is that CXF 3.1 will no longer support Java 6 and will require Java 7 or n ...

  2. NSURLConnection下载

    @interface AppDelegate () <NSURLConnectionDataDelegate> {    NSMutableData *mData;} @end @impl ...

  3. 深入理解offsetTop与offsetLeft

    做为走上前端不归路的我,以前只是认为offsetTop是元素的左边框至包含元素offsetParent的左内边框之间的像素距离,同理offsetRight是相对于上内边框.那么问题来了,包含元素off ...

  4. SCXML和QScxml使用总结

    最近接触了SCXML这个状态描述文本,简单来讲就是描述了整个状态的变迁过程的一种XML格式的表格.Qt labs中有一个项目就是QScxml,它基于QStateMachine上层制作,可以直接读取SC ...

  5. Linux 0.11下信号量的实现和应用

    Linux 011下信号量的实现和应用 生产者-消费者问题 实现信号量 信号量的代码实现 关于sem_wait和sem_post sem_wait和sem_post函数的代码实现 信号量的完整代码 实 ...

  6. [Git]Git远程仓库

    1.创建ssh key 查看主目录下面 C:\Users\Administrator\.ssh 是否存在 id_rsa 和 id_rsa.pub 文件,如果不存在需要generate new key. ...

  7. strtolower() strtoupper()等字符串大小写转换函数

    $str = "Mary Had A Little Lamb and She LOVED It So"; string strtolower ( string $str )— 将字 ...

  8. linux c数据库备份第四版

    该版本算是比较成熟的啦,欢迎大伙拿来试用!!!1.新增数据库连接和备份时间配置文件conf2.新增日志文件,程序运行的一些异常会记录在log文件下 后续的工作:1.将代码切割为多个文件,分类存放代码2 ...

  9. FFT快速傅立叶

    Description 给出两个n位10进制整数x和y,你需要计算x*y. Input 第一行一个正整数n.第二行描述一个位数为n的正整数x.第三行描述一个位数为n的正整数y. Output 输出一行 ...

  10. Sample RWD Setup for Client-Side Development

    RWD and RESS concepts fluid images, responsive grids, and media queries. Twitter's Bootstrap based o ...