通過iphone學習連接到ssl web服務的最佳起點是什麼?iPhone - SSL連接
到目前爲止,我通過SOAP等通過http做了一些基本的連接,但我對https沒有經驗。任何良好的來源,教程,起始參考,「使用nsurl ...類」的讚賞
通過iphone學習連接到ssl web服務的最佳起點是什麼?iPhone - SSL連接
到目前爲止,我通過SOAP等通過http做了一些基本的連接,但我對https沒有經驗。任何良好的來源,教程,起始參考,「使用nsurl ...類」的讚賞
NSURLConnection默認情況下使用SSL和可以訪問https站點。可能會出現有關讓用戶信任SSL證書的問題,here對此的討論我發現很有趣。
結賬ASIHTTPRequest。它是非常穩定,無泄漏,易於使用,包括一堆好東西,如下載文件恢復,進度條支持等...它也有認證支持
我發佈了一個示例https客戶端。它會忽略服務器證書無效。 服務器具有webget法uritemplate =用戶名({用戶代碼})/密碼({密碼})
可以使用CharlesProxy檢查您的傳出消息
#import "Hello_SOAPViewController.h"
@interface NSURLRequest (withHttpsCertificates)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
@end
@implementation Hello_SOAPViewController
NSMutableData *webData;
- (void)viewDidLoad {
//////////////////////////////////////////////////
//Web Service Call
//////////////////////////////////////////////////
NSURL *url = [NSURL URLWithString:@"https://192.168.1.105/HelloService/Service.svc/username(user)/password(xxx)"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
[theRequest addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[theRequest setHTTPMethod:@"GET"];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(theConnection) {
webData = [[NSMutableData data] retain];
}
else {
NSLog(@"theConnection is NULL");
}
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR with theConnection:%@",[error description]);
if ([error code] == -1001){//isEqualToString:@"timed out"]) {
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Connection Error" message:@"Server Unresponsive" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
[alertView show];
}else{
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Connection Error" message:@"Check your internet connection " delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
[alertView show];
}
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received Bytes: %d", [webData length]);
///////////////////////
//Process Your Data here:
///////////////////////
[connection release];
[webData release];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
是身份驗證安全加密或如何我能理解這一點嗎?謝謝 – 2012-10-13 15:57:23
這是一個非常古老的答案,ASIHTTPRequest不再被維護。嘗試https://github.com/AFNetworking/AFNetworking – coneybeare 2012-10-14 14:41:16