iOS --NSURLConnection 请求总结

1.NSURLConnection 异步请求。

举例,下载图片。

- (void)viewDidLoad {

    [superviewDidLoad];

        NSURL *urlString = [NSURL       URLWithString:@"http://www.pptbz.com/pptpic/UploadFiles_6909/201110/20111014111307895.jpg"]; //创建请求路径。

        

        NSOperationQueue *queue = [[NSOperationQueuealloc]init];

        

        NSURLRequest *request = [NSURLRequestrequestWithURL:urlString];//创建请求对象。

        [NSURLConnection sendAsynchronousRequest:request queue:queuecompletionHandler:^(NSURLResponse *_Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

                UIImage *image = [UIImageimageWithData:data];

                NSLog(@"%@ --- response :%@",image,response);//打印response查看响应体的内容。

                

        }];//发送请求方法。


/* Designated initializer */

//主要有三种发起请求的方法

- (nullable instancetype)initWithRequest:(NSURLRequest *)request delegate:(nullableid)delegate startImmediately:(BOOL)startImmediately;//这个方法不是立即启动需要手动启动,start。


- (nullable instancetype)initWithRequest:(NSURLRequest *)request delegate:(nullableid)delegate;

+ (nullable NSURLConnection*)connectionWithRequest:(NSURLRequest *)request delegate:(nullableid)delegate ;


}

2.Response响应体的内容。

{ URL: http://www.pptbz.com/pptpic/UploadFiles_6909/201110/20111014111307895.jpg } { status code: 200, headers {

    "Accept-Ranges" = bytes;

    Age = 1;

    Connection = "keep-alive";

    "Content-Length" = 66572;// 图片的大小。

    "Content-Type" = "image/jpeg";//传输的文件格式。

    Date = "Tue, 02 Aug 2016 03:37:02 GMT";

    Etag = "\"0ba55321f8acc1:2ba\"";

    "Last-Modified" = "Fri, 14 Oct 2011 03:13:08 GMT";

    Server = "Microsoft-IIS/6.0";

    "X-Via" = "1.1 xzai40:0 (Cdn Cache Server V2.0), 1.1 wt197:10 (Cdn Cache Server V2.0)";

} }


3.NSURLConnection GET 请求方法:

NSURL *urlString = [NSURL URLWithString:@"http://www.pptbz.com?username=%@&pwd=%@",self.name.text,self.pwd.text];


4.NSURLConnection 请求代理方法:

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

        NSLog(@"%s",__func__);//超时会执行这个方法。

}


-(void)connection:(NSURLConnection *)connection didReceiveData:(nonnullNSData *)data{

        

        

        

        _responseData = [NSMutableDatadata];

        [_responseData appendData:data];

        [self showProgress];

        NSLog(@"%s,%@",__func__,data);

        

}


-(void)connection:(NSURLConnection *)connection didReceiveResponse:(nonnullNSURLResponse *)response{

        _totalData = response.expectedContentLength;

        NSLog(@"%s,%lld",__func__,response.expectedContentLength);

}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

        NSLog(@"%s",__func__);

        

        

//请求完成在这里更新数据。        

        [self showImage:_responseData];

}



-(void)showProgress{

        _progress = [[UIProgressViewalloc]initWithFrame:CGRectMake(10,20, self.view.bounds.size.width,20)];

        _progress.progressViewStyle =UIProgressViewStyleDefault;

        _progress.progress = [self.responseDatalength]*1.0 / _totalData;

        [self.viewaddSubview:_progress];

}


NSURLConnection可以用于下载文件, response. expectedContentLength可以提前获取文件的整体大小,然后通过progress View显示进度条。


5.NSURLConnection POST请求:

   

        NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:urlString];

        

        request.HTTPMethod = @"POST";

        request.HTTPBody = [@"username=&pwd="dataUsingEncoding:NSUTF8StringEncoding];

request.timeoutInterval =5.0;

        [request setValue:@"iOS 9.0"forHTTPHeaderField:@"User-Agent"];//告诉服务器该设备是什么类型。


此方法可以用在SOAP请求,指定请求方法和请求的数据类型。


6.中文转码,UTF8.

7.取消请求,cancel。

8.NSString -> NSData

NSData *data = [@"xxxx" dataUsingEncoding:NSUTF8StringEncoding];

NSString *sting = [[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding];







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值