You are on page 1of 15

HTTP

AFNetworking
7. predavanje

HTTP

<protokol>://<server>/<path>

HTTP karakteristike

Request - response

alje se request u odreenom formatu

Server odgovara sa response-om

Request ima rok trajanja (timeout)

Svaki request ima URI (Uniform Resource


Identifier)

HTTP Request format

Request line: GET /index.html HTTP/1.0

Header lines:

Message body: Content-lenght:

Host: www.example.com
Date: Tue, 24 Feb 2015 08:59:35 GMT
User-Agent: Mozilla/5.0(en)
Accept-Language: en-us
String of the body

Request methods

GET - Dovlacenje statickih podataka, ili podataka


na koje mi nasim requestom ne uticemo direktno.
GET parametri mogu da se koriste kao filter.

GET /api/users - dovlaci niz usera. GET /api/


users/1 - dovlaci usera sa ID-em 1

POST - Slanje informacija na server, tj kreiranje


nekog objekta. POST /api/users kreira novog usera.
POST request params se koriste da prosledimo
detalje o objektu koji kreiramo.

PUT - Apdejtovanje postojeceg resursa, npr PUT /


api/users/1 sa request params objektom,
apdejtovace usera sa id-em 1.

DELETE - Brisalje postojeceg resursa, npr


DELETE /api/users/1 obrisace objekat sa tim ID-em

Result codes

1xx: hold on // Informaciona poruka, nije jos zavrsena

2xx: here you go // Uspesno zavrseno

3xx: go away // Redirection

4xx: you fucked up // Client error

5xx: I fucked up // Server error

200 OK

204 No Content

300 Multiple Choices

301 Moved Permanently

302 Moved Temporarily

304 Not Modified

400 Bad Request

401 Unauthorized

404 Not Found

500 Internal server error

Cookies

HTTP is stateless protokol

Cookies se salju kao plain text (security issue)

Mogu se koristiti za autentikaciju

AFNetworking 2.0

Instalacija:

Dodati pod AFNetworking' u Podfile

Iz Terminala otkucati pod install, u istom


direktorijumu gde je i podfile

Kada Cocoapods zavrsi, otvorite Workspace fajl, a


ne project file!

GET - AFNetworking 2.0


AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager
manager];
[manager GET:@"http://api.openweathermap.org/data/2.5/weather"
parameters:@{@"q":@"Belgrade,rs"} success:^(AFHTTPRequestOperation *operation,
NSDictionary *responseObject) {
NSLog(@"JSON: %@", [[responseObject objectForKey:@"main"]
objectForKey:@"temp"]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

POST - URL-Form-Encoded
Request
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager
manager];
NSDictionary *parameters = @{@"foo": @"bar"};
[manager POST:@"http://example.com/resources.json" parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

POST - Multi-Part Request


AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager
manager];
NSDictionary *parameters = @{@"foo": @"bar"};
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
[manager POST:@"http://example.com/resources.json" parameters:parameters
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePath name:@"image" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

Reachability
NSURL *baseURL = [NSURL URLWithString:@"http://example.com/"];
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager
alloc] initWithBaseURL:baseURL];
NSOperationQueue *operationQueue = manager.operationQueue;
[manager.reachabilityManager
setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusReachableViaWWAN:
case AFNetworkReachabilityStatusReachableViaWiFi:
[operationQueue setSuspended:NO];
break;
case AFNetworkReachabilityStatusNotReachable:
default:
[operationQueue setSuspended:YES];
break;
}
}];
[manager.reachabilityManager startMonitoring];

iOS9 - App Transport


Security
Info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>yourserver.com</key>
<dict>
<!--Include to allow subdomains-->
<key>NSIncludesSubdomains</key>
<true/>
<!--Include to allow HTTP requests-->
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<!--Include to specify minimum TLS version-->
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
Note: https://developer.apple.com/library/prerelease/ios/technotes/AppTransport-Security-Technote/