我正在努力使用https请求Web服务.我收到这样的错误:
An error occured : The certificate for this server is invalid. You might be connecting to a server that is pretending to be “SERVER_ADDRESS” which could put your confidential information at risk.
Run Code Online (Sandbox Code Playgroud)
我没有使用NSUrlConnectionDelegate.这是我的方法:
- (void)sendRequestWithUrl:(NSURL*)url block:(void (^)(NSDictionary *dict, NSError *error)) block{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
NSError* err = nil;
NSHTTPURLResponse* rsp = nil;
// Perform the request synchronously on this thread
NSData *rspData = [NSURLConnection sendSynchronousRequest:request returningResponse:&rsp error:&err];
if (rspData && …Run Code Online (Sandbox Code Playgroud) 我开发了一个发送HTTPS请求的iPhone框架,以便与公共可用的后端服务器进行通信.目前,我对不受信任的服务器证书存在很大问题.
后端服务器的证书未由受信任的CA签名,因此我的第一种方法是使用NSURLRequest的私有allowAnyHTTPSCertificateForHost.虽然这可以按预期工作,并且作为临时解决方案很好,但我们的客户要求最终结果是干净的解决方案.因此,我编写了一种方法,允许从钥匙串中的文件系统安装提供的证书,但此方法在iPhone模拟器中无法正常工作.证书安装在主机的Mac OS X钥匙串中.不幸的是,如果我调用NSURLConnection的sendSynchronousRequest方法,我会检索"不受信任的服务器证书"错误.好像NSURLConnection无法访问主机的Mac OS X钥匙串来检索证书.
我的猜测是正确还是我错过了什么?
如果我在真正的iPhone设备上运行我的应用程序(我还没有可用的),我的方法会工作吗?
iPhone模拟器中是否存在钥匙串?
是否可以在iPhone模拟器上向具有不受信任证书的服务器发送HTTPS请求,或者我是否必须使用预编译器指令来实现不同的例程,具体取决于底层平台(模拟器或设备)?
任何帮助都非常感谢.
谢谢,马蒂亚斯
第三方应用程序可以访问iPhone的钥匙串以便添加X509证书吗?如果是,怎么办?
如果没有,它可以访问钥匙串只是为了从中读取证书吗?
基本上,我需要的是:
1)我的应用程序需要访问https站点,该站点使用未由任何可信CA签名的证书.当尝试通过https连接时,我得到一个例外.
2)如果我能以编程方式将根证书添加到钥匙串中,那就太棒了; 如果用户可以通过Safari访问该站点,接受其证书,然后使用我的应用程序访问该站点就足够了.
到目前为止,我一直在使用以下界面来超越https:
@interface NSURLRequest (DummyInterface)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
@end
Run Code Online (Sandbox Code Playgroud)
但这不是我想要的.
有什么建议?
我正在使用AFNetworking开发建立HTTPS连接的iPhone应用程序,我想使用我可以定义的自签名证书或可信证书,但我在AFNetworking中找不到任何功能(API)可以做到这一点.
我看到了这个页面:如何使用NSURLConnection连接SSL以获取不受信任的证书?.但我想知道如何分配cer文件?
谢谢.
我想在iphone模拟器上测试一个应用程序,它使用由我们自己的CA签名的证书连接到服务.我可以通过添加具有CA证书的配置文件在实际设备上执行此操作.我原以为在标准OSX钥匙串中使用CA证书会有效,但事实并非如此.
所以我可以在没有任何警告的情况下通过Safari访问该服务,但是在尝试在模拟器中运行时我收到错误.
我正在编写一个iPhone应用程序,我想连接到"HTTPS"服务器以获取一些信息.但是,我在控制台中得到错误
NSUnderlyingError =错误域= kCFErrorDomainCFNetwork代码= -1202 UserInfo = 0x3e95cf0"此服务器的证书无效.您可能连接到假装为"example.com"的服务器,这可能会使您的机密信息面临风险."; }
我如何信任证书并获取http状态代码200
以下是我的代码.
NSMutableURLRequest *request_get2 = [[[NSMutableURLRequest alloc] init] autorelease];
[request_get2 setURL:[NSURL URLWithString:@"https://www.example.com"]];
[request_get2 setHTTPMethod:@"GET"];
[request_get2 setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//[request_get2 setValue:@"text/html; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request_get2 setValue:@"https://www.example.com" forHTTPHeaderField:@"Referer"];
[request_get2 setHTTPShouldHandleCookies:YES];
// cookiesString is in format "cookieName=cookieValue;"
[request_get2 setValue: (NSString *) cookiesString forHTTPHeaderField:@"Cookie"];
// doGet - response
NSHTTPURLResponse *response_get2 = nil; // it change from 'NSURLRespaonse' to 'NSHTTPURLResponse'
NSError *error_get2 = nil;
NSData *responseData_get2 = [NSURLConnection sendSynchronousRequest:request_get2 returningResponse:&response_get2 error:&error_get2];
NSString *data_get2 = [[NSString alloc]initWithData:responseData_get2 encoding:NSUTF8StringEncoding];
if …Run Code Online (Sandbox Code Playgroud) 我正在尝试将Android应用程序移植到IOS平台.我从位于Apache Tomcat服务器中的服务获取数据,最近他们在服务上添加了安全性.安全概述是
Android版本没有问题.在添加安全性之前,我正在执行简单的http请求,但现在我需要使用我的凭据执行POST请求,因此我总是收到error.html.我远不是网络编程方面的专家.
这是Android http客户端设置代码:
if(httpClient==null)
{
DefaultHttpClient client = new DefaultHttpClient();
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("https", sf, 8443));
mgr = new ThreadSafeClientConnManager(client.getParams(), registry);
int timeoutConnection = 5000;
HttpConnectionParams.setConnectionTimeout(client.getParams(), timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 7000;
HttpConnectionParams.setSoTimeout(client.getParams(), timeoutSocket); …Run Code Online (Sandbox Code Playgroud) 我正在检查以下错误:错误域= NSURLErrorDomain代码= -1202"此服务器的证书无效.您可能正在连接到假装为"api.linkedin.com"的服务器,这可能会使您的机密信息面临风险".的UserInfo = 0x1c53e630
这在模拟器上工作正常,但在设备上它给出了上述错误.
请帮忙纠正它.
我对Objective-C有点新,但遇到了一个我无法解决的问题,主要是因为我不确定我是否正确实现了解决方案.
我正在尝试使用同步连接连接到具有自签名证书的https站点.我得到了
错误域= NSURLErrorDomain代码= -1202"不受信任的服务器证书"
我在这个论坛上看到了一些解决方案的错误.我找到的解决方案是添加:
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}
Run Code Online (Sandbox Code Playgroud)
到NSURLDelegate接受所有证书.当我使用以下内容连接到网站时:
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://examplesite.com/"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
Run Code Online (Sandbox Code Playgroud)
它工作正常,我看到挑战被接受.但是,当我尝试使用同步连接进行连接时,我仍然会收到错误,并且当我输入日志记录时,我看不到调用挑战函数.
如何使用同步连接来使用质询方法?是否与委托有关:URLConnection的自我部分?我还有在NSURLDelegate中发送/接收数据的日志记录,该数据由我的连接函数调用,但不是由同步函数调用.
我用于同步部分的内容:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:@"https://examplesite.com/"]];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: [[NSString stringWithString:@"username=mike"] dataUsingEncoding: NSUTF8StringEncoding]];
dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"%@", error);
stringReply = [[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding];
NSLog(@"%@", stringReply);
[stringReply release];
NSLog(@"Done");
Run Code Online (Sandbox Code Playgroud)
就像我提到的,我对目标C有点新意,所以要善待:)
谢谢你的帮助.麦克风
我正在使用Xcode 7,Swift 2和iOS9.我想使用NSURLSession连接到Web服务,但是当我尝试连接时出现以下错误:
2015-10-13 16:07:33.595 XCTRunner[89220:4520715] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
2015-10-13 16:07:33.604 XCTRunner[89220:4520571] Error with connection, details: Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “domainapi.com” which could put your confidential information at risk." UserInfo={NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x7fac7b6facc0>, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?,
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
func request( dataPost : String, successHandler: (response: String) -> Void)-> String {
let destination:String = "https://domainapi.com:8743/WebService/sendData"
let request …Run Code Online (Sandbox Code Playgroud) 我试图通过使用以下代码发出https发布请求.
NSURL *url = [NSURL URLWithString:@"https://portkey.formspring.me/login/"];
//initialize a request from url
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url standardizedURL]];
//set http method
[request setHTTPMethod:@"POST"];
//initialize a post data
NSDictionary *postDict = [NSDictionary dictionaryWithObjectsAndKeys:@"username", @"username",
@"password", @"password", nil];
NSError *error=nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:postDict
options:NSJSONWritingPrettyPrinted error:&error];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
//set post data of request
[request setHTTPBody:jsonData];
//initialize a connection from request
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
//start the connection
[connection start];
Run Code Online (Sandbox Code Playgroud)
但是我得到了回应.
error Error Domain = NSURLErrorDomain Code = …
我正在使用[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)]从Web服务提取数据,但Web服务器具有自行颁发的证书,导致出现此错误:

显示错误:
NSAlert *a = [NSAlert alertWithError:error];
[a runModal];
Run Code Online (Sandbox Code Playgroud)
有没有办法忽略这个错误并继续?
我的问题是,我想在我的IOS应用程序中使用ssl连接.服务器中有自签名的ssl.在我的iPhone应用程序(任何配置)中有什么事情可做或只是我将使用https而不是http访问我的网址.感谢您的回答.