Skip to content

Instantly share code, notes, and snippets.

@RandomEtc
Created May 16, 2012 21:56
Show Gist options
  • Save RandomEtc/2714281 to your computer and use it in GitHub Desktop.
Save RandomEtc/2714281 to your computer and use it in GitHub Desktop.
Minimal Objective-C HTTP client for testing UTF8 decoding issues
#import <Foundation/Foundation.h>
#import <Foundation/NSURLRequest.h>
int main (void)
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
// Robert Hodgin's tweet with emoji:
// https://twitter.com/#!/flight404/statuses/165946274644885504
// NSURL* nsUrl = [NSURL URLWithString:@"https://api.twitter.com/1/statuses/show.json?id=165946274644885504&include_entities=true"];
NSURL* nsUrl = [NSURL URLWithString:@"http://localhost:1337/1/statuses/show/165946274644885504.json"];
NSMutableURLRequest* req = [[NSMutableURLRequest alloc] initWithURL:nsUrl];
[req setHTTPMethod: @"GET"];
req.HTTPShouldHandleCookies = NO;
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:req
returningResponse:&response
error:&error];
if (response && response.statusCode == 200) {
NSString *strrsp = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
if (strrsp) {
NSLog(@"Tweet successfully decoded as UTF8: %@", strrsp);
} else {
NSLog(@"Unable to decode data into string.");
}
} else {
NSLog(@"HTTP Error description - %@ (%ld): %@", error.domain, (long)error.code, [error localizedDescription]);
}
[pool release];
return 0;
}
CC = clang
MAIN = main
SRCS = main.o
default: $(MAIN)
$(MAIN): $(SRCS)
$(CC) -O0 -Wall -o $(MAIN) $(SRCS) -framework Foundation
clean:
find . -name "*.o" -exec rm {} \;
if [ -f "$(MAIN)" ]; then rm $(MAIN); fi
all: default
@RandomEtc
Copy link
Author

See https://gist.github.com/2714295 for the server this is intended to (not) work with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment