自己証明書だと、通信に失敗するでの軒並みOKにするには下のコードで行ける!!
おすすめはしない!!
※ コマンドラインでしか試してません ※
- (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]; }
リンク
参考またはこうすべきサイトサンプルコード
This file contains 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
// | |
// 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; | |
} |
0 件のコメント:
コメントを投稿