Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 02ae08122e
Fetching contributors…

Cannot retrieve contributors at this time

file 84 lines (57 sloc) 2.779 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
//
// MyDocument.h
// curly
//
// Created by Robert Righter on 2/6/10.
//

#import "Requestor.h"


@implementation Requestor

- (void) doRequestWith:(NSString*)poststring successAction:(SEL)success failureAction:(SEL)failure target:(id)target url:(NSString *)url {

updateSuccess = success;
updateFailure = failure;
callbackTarget = target;
responseHeaders = [[NSMutableString alloc] init];
responseData = [[NSMutableData data] retain];

NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];

if (poststring == nil) {
[request setHTTPMethod:@"GET"];
}
else{
[request setHTTPMethod:@"POST"];
//[request setHTTPBody:[[Requestor urlEncodeValue:poststring] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:[poststring dataUsingEncoding:NSUTF8StringEncoding]];
}

//send off the request
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}



+ (NSString *)urlEncodeValue:(NSString *)str {
NSString *result = (NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)str, NULL, CFSTR(":/?#[]@!$&’()*+,;="), kCFStringEncodingUTF8);
return [result autorelease];
}



- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    [responseData setLength:0];
NSLog(@"Got response with headers: %@",[((NSHTTPURLResponse *)response) allHeaderFields]);

NSDictionary *headers = [((NSHTTPURLResponse *)response) allHeaderFields];

NSString *key;
for (key in headers) {
[responseHeaders appendString:[NSString stringWithFormat:@"%@ : %@\n", key, [headers valueForKey:key]]];
}
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    [callbackTarget performSelector:updateFailure];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // Once this method is invoked, "responseData" contains the complete result
//NSLog(@"%@",[[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding]);
//TODO: Should probably check and make sure that responseData actually has a success message in it (IE just because it didnt 500 error doesnt mean everything is perfect
//NSLog(@"MADE IT HERE 0: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:responseHeaders forKey:@"headers"];
[dictionary setObject:[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] forKey:@"body"];
[callbackTarget performSelector:updateSuccess withObject:dictionary];
}







@end
Something went wrong with that request. Please try again.