Created
September 18, 2013 23:05
-
-
Save goliatone/6616995 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GServiceGateway.h | |
// | |
// | |
#import <Foundation/Foundation.h> | |
@interface GServiceGateway : NSObject | |
- (BOOL)connectionPOST:(NSString *)url | |
withParams:(NSDictionary *)aDictionary; | |
- (BOOL)connectionPOST:(NSString *)url | |
withString:(NSString *)aString; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GServiceGateway.m | |
// | |
#import "GServiceGateway.h" | |
@implementation GServiceGateway | |
- (BOOL)connectionPOST:(NSString *)url | |
withParams:(NSDictionary *)aDictionary { | |
if ([aDictionary count] > 0) { | |
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] | |
initWithURL:[NSURL URLWithString:url] | |
]; | |
[request setHTTPMethod:@"POST"]; | |
NSMutableString *postString = [[NSMutableString alloc] init]; | |
NSArray *allKeys = [aDictionary allKeys]; | |
for (int i = 0; i < [allKeys count]; i++) { | |
NSString *key = [allKeys objectAtIndex:i]; | |
NSString *value = [aDictionary objectForKey:key]; | |
[postString appendFormat:( (i == 0) ? @"%@=%@" : @"&%@=%@" ), key, value]; | |
} | |
NSString *postLength = [NSString stringWithFormat:@"%d", [postString length]]; | |
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; | |
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; | |
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]]; | |
[NSURLConnection connectionWithRequest:request delegate:self]; | |
postString = nil; | |
request = nil; | |
return YES; | |
} else { | |
return NO; | |
} | |
} | |
- (BOOL)connectionPOST:(NSString *)url | |
withString:(NSString *)aString { | |
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] | |
initWithURL:[NSURL URLWithString:url] | |
]; | |
[request setHTTPMethod:@"POST"]; | |
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; | |
NSString *postLength = [NSString stringWithFormat:@"%d", [aString length]]; | |
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; | |
[request setHTTPBody:[aString dataUsingEncoding:NSUTF8StringEncoding]]; | |
[NSURLConnection connectionWithRequest:request delegate:self]; | |
request = nil; | |
return YES; | |
} | |
@end |
Author
goliatone
commented
Sep 18, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment