Skip to content

Instantly share code, notes, and snippets.

@YooWaan
Created January 25, 2012 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YooWaan/1675763 to your computer and use it in GitHub Desktop.
Save YooWaan/1675763 to your computer and use it in GitHub Desktop.
NSURLConnectionDelegate for self signed
//
// Description
// ----------------
//
// This is sample for handle self sigined certificate.
//
// This code have 3 way for building.
//
// - Default : Can't connect self signed certificate server.
// - AUTH : Can connect self signed certificate server by old style.
// - AUTH_NEW : Can connect self signed certificate server by new style.
//
// Command usage
// ----------------
//
// ./urlcon http(s)://xxxxx.com/
//
//
// Compile command
// ----------------
//
// Default : not handle authenticate
//
// clang -g -Wall -fobjc-arc -o urlcon web.m -framework Foundation
//
//
// AUTH : handle old style
//
// clang -g -Wall -fobjc-arc -o urlcon web.m -framework Foundation -DAUTH
//
//
// AUTH_NEW : handle new style
//
// clang -g -Wall -fobjc-arc -o urlcon web.m -framework Foundation -DAUTH_NEW
//
//
// Note
// ------
//
// Perhaps iOS 5 can't support DSA signature algorism.
// Mac is running this code.
//
//
#import <Foundation/Foundation.h>
@interface HTTP : NSObject <NSURLConnectionDelegate>
{
BOOL finished;
}
@property (nonatomic) BOOL ssl;
-(void) connect:(NSString*)url;
@end
@implementation HTTP
@synthesize ssl;
-(id) init {
if ((self = [super init]) != nil) {
finished = NO;
}
return self;
}
-(void) connect:(NSString*)url {
NSMutableString* urlString = [NSMutableString stringWithCapacity:128];
[urlString appendString:url];
NSLog(@"URL[%@]", urlString);
[NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] delegate: self];
NSRunLoop* loop = [NSRunLoop currentRunLoop];
while (finished == NO && [loop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture] ]);
}
#pragma mark --
#pragma mark Delegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"000>%@", response);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"--->: %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
finished = YES;
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
finished = YES;
NSLog(@"err->%@", error);
}
#ifdef AUTH
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return YES;
}
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection {
return YES;
}
- (void) connection:(NSURLConnection *)conn didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSURLProtectionSpace * protectionSpace = [challenge protectionSpace];
NSURLCredential* credentail = [NSURLCredential credentialForTrust:[protectionSpace serverTrust]];
[[challenge sender] useCredential:credentail forAuthenticationChallenge:challenge];
}
#endif
#ifdef AUTH_NEW
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSURLProtectionSpace * protectionSpace = [challenge protectionSpace];
NSURLCredential* credentail = [NSURLCredential credentialForTrust:[protectionSpace serverTrust]];
[[challenge sender] useCredential:credentail forAuthenticationChallenge:challenge];
}
#endif
@end
int main(int argc, char* argv[]) {
@autoreleasepool {
NSString* url = [NSString stringWithCString:argv[1] encoding:NSUTF8StringEncoding];
HTTP* https = [[HTTP alloc] init];
[https connect:url];
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment