iOS网络篇就是ios用的iOS大神养成记

网络编程基础3

2015-06-01  本文已影响2078人  董军1990

网络基础编程3

http长用的方法

GET

POST

GET 缓存

GET 方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSURL *url = [NSURL URLWithString:@"http://localhost/itcast/images/head1.png"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:1 timeoutInterval:10.0];

    if (self.etag.length > 0) {
        NSLog(@"%@", self.etag);

        [request setValue:self.etag forHTTPHeaderField:@"If-None-Match"];
    }

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

        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
        NSLog(@"%@", httpResponse);

        // 判断服务器返回的状态码,是否是 304
        if (httpResponse.statusCode == 304) {
            NSLog(@"加载缓存数据");

            NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
            self.iconView.image = [UIImage imageWithData:cachedResponse.data];

            return;
        }

        self.etag = httpResponse.allHeaderFields[@"Etag"];
        self.iconView.image = [UIImage imageWithData:data];
    }];
}

Request 缓存请求头

设置缓存

在appdelegate里边设置网络缓存的大小

NSURLCache *urlCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 diskCapacity:20 * 1024 * 1024 diskPath:nil];
[NSURLCache setSharedURLCache:urlCache];
GET & POST 登录
基本代码
GET 登录
// MARK: - GET 登录
- (void)getLogin {
    NSString *username = @"zhangsan";
    NSString *pwd = @"zhang";

    NSString *urlString = [NSString stringWithFormat:@"http://localhost/login.php?username=%@&password=%@", username, pwd];
    NSURL *url = [NSURL URLWithString:urlString];

    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:2 timeoutInterval:10.0];

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

        id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];

        NSLog(@"%@ - %@", response, result);
    }];
}
POST 登录
// MARK: POST 登录
- (void)postLogin {
    NSString *username = @"zhangsan";
    NSString *pwd = @"zhang";

    NSURL *url = [NSURL URLWithString:@"http://localhost/login.php"];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:2 timeoutInterval:10.0];
    request.HTTPMethod = @"POST";
    NSString *bodyString = [NSString stringWithFormat:@"username=%@&password=%@", username, pwd];
    request.HTTPBody = [bodyString dataUsingEncoding:NSUTF8StringEncoding];

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

        id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];

        NSLog(@"%@ - %@", response, result);
    }];
}

GET & POST 对比

URL

Request
Connection

GET & POST 补充

百分号转义

urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
缓存(知道就好)

缓存

总结:通过本篇博客我们学习到了get,post登陆,和get,post缓存的设置,和get我们从网络上加载数据实时更新,因为默认是从网络上加载数据,我们需要设置一个If-None-Match的值,post没有本地缓存所以我们不需要设置,也学习到了get,post请求的一些区别,get的请求速度快,但是不安全,post的把所有的数据都封装在请求头里边所以比较安全

上一篇 下一篇

猜你喜欢

热点阅读