【发布时间】:2009-11-25 20:11:42
【问题描述】:
学习通过 iphone 连接到 ssl Web 服务的最佳起点是什么?
到目前为止,我通过 SOAP 等通过 http 进行了一些基本连接,但我没有使用 https 的经验。任何好的资源、教程、起始参考、“使用 nsurl...class”都表示赞赏
【问题讨论】:
标签: iphone cocoa-touch web-services ssl
学习通过 iphone 连接到 ssl Web 服务的最佳起点是什么?
到目前为止,我通过 SOAP 等通过 http 进行了一些基本连接,但我没有使用 https 的经验。任何好的资源、教程、起始参考、“使用 nsurl...class”都表示赞赏
【问题讨论】:
标签: iphone cocoa-touch web-services ssl
NSURLConnection 默认使用 SSL,可以访问 https 站点。关于让用户信任 SSL 证书可能会出现问题,here 对此进行了讨论,我发现这很有趣。
【讨论】:
我正在发布一个示例 https 客户端。如果服务器证书无效,它会忽略。 服务器有一个带有 uritemplate=username({usercode})/password({passcode})
的 webget 方法您可以使用 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];
}
【讨论】:
查看ASIHTTPRequest。它非常稳定、无泄漏、易于使用,并且包括许多好东西,例如下载文件恢复、进度条支持等……它还支持身份验证
【讨论】: