Skip to content

Instantly share code, notes, and snippets.

@yagihiro
Created April 10, 2011 02:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yagihiro/911999 to your computer and use it in GitHub Desktop.
Save yagihiro/911999 to your computer and use it in GitHub Desktop.
Objective-C で簡単に HTTP 通信を取り扱うことができるようにするクラス
//
// HttpConnectionWrapper.h
//
// Created by Hiroki Yagita on 11/04/09.
// Copyright 2011 Hiroki Yagita. All rights reserved.
//
/*! @file */
#import <Foundation/Foundation.h>
@class HttpConnectionWrapper;
/*!
HttpConnectionWrapper クラスのユーザーがリクエストの結果を受け取りたい時に実装してください。
*/
@protocol HttpConnectionWrapperDelegate
-(void)didReceiveAllResponse:(HttpConnectionWrapper *)connection withString:(NSString *)string;
@end
/*!
HTTP 通信を簡単に扱えるようにするためのラッパークラスです。
@see HttpConnectionWrapperDelegate
*/
@interface HttpConnectionWrapper : NSObject {
NSMutableString *payload;
id<HttpConnectionWrapperDelegate> delegate;
}
@property (nonatomic, retain) NSMutableString *payload;
@property (nonatomic, assign) id<HttpConnectionWrapperDelegate> delegate;
/*!
HTTP 通信を開始します。結果を受け取る場合は HttpConnectionWrapperDelegate デリゲートを実装してください。
@param urlStr URL文字列を指定してください。
@see HttpConnectionWrapperDelegate
*/
-(void)startWithString:(NSString *)urlStr;
@end
//
// HttpConnectionWrapper.h
//
// Created by Hiroki Yagita on 11/04/09.
// Copyright 2011 Hiroki Yagita. All rights reserved.
//
#import "HttpConnectionWrapper.h"
@implementation HttpConnectionWrapper
@synthesize payload, delegate;
-(void)startWithString:(NSString *)urlStr {
NSLog(@"%s %@", __FUNCTION__, urlStr);
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
NSLog(@"%s connection started.", __FUNCTION__);
self.payload = [[[NSMutableString alloc] initWithCapacity:65536] autorelease];
} else {
NSLog(@"%s connection failed.", __FUNCTION__);
}
}
#pragma mark -
#pragma mark NSURLConnection delegate
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSLog(@"%s connection:%@ response:%@", __FUNCTION__, connection, [httpResponse allHeaderFields]);
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"%s connection:%@", __FUNCTION__, connection);
// TODO: encoding must be decide response header.
//NSJapaneseEUCStringEncoding
NSString *s = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
if (s) [self.payload appendString:s];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"%s connection:%@", __FUNCTION__, connection);
[self.delegate didReceiveAllResponse:self withString:self.payload];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment