目前我在ios中使用块消费肥皂网服务我的源代码如下
NSString *xml = requestXMLToSent;
NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[xml length]];
NSURL *serviceURL = [NSURL URLWithString: url];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:serviceURL];
[urlRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[urlRequest addValue: serviceURL forHTTPHeaderField:@"SOAPAction"];
[urlRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[urlRequest setHTTPBody: [xml dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setHTTPMethod:@"POST"];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError == NULL) {
NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;
NSInteger statuscode = httpResponse.statusCode;
if (statuscode == 200) {
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"response String : %@",responseString);
}else{
NSLog(@"%@",response);
}
}else{
NSLog(@"There is an error in URL connection and the Error is : %@",connectionError);
}
我收到以下错误@ console
NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
URL连接有错误,错误是:错误域= NSURLErrorDomain代码= -1202“此服务器的证书无效。您可能正在连接到假装为”www.xxxxxxxx.net的服务器“这可能会使您的机密信息面临风险。” UserInfo = 0x10948bbb0 {NSUnderlyingError = 0x109470d10“此服务器的证书无效。您可能连接到假冒”www.xxxxxx.net“的服务器,这可能会使您的机密信息面临风险。”,NSErrorFailingURLStringKey = https: // www .----------------------------------,NSErrorFailingURLKey = https:// ----- -------------------- NSLocalizedRecoverySuggestion =您是否还要连接到服务器?,NSURLErrorFailingURLPeerTrustErrorKey =,NSLocalizedDescription =此服务器的证书无效。您可能正在连接到假装为“www.xxxxxx.net”的服务器,这可能会使您的机密信息面临风险。}
答案 0 :(得分:12)
服务器正在抛出 SSL 证书错误。
为了测试,您可以将以下代码添加到appDelegate:
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host {
return YES;
}
这将绕过SSL错误
注意:适用于NSURLConnection& UIWebView,但不适用于WKWebView
<强>编辑:强>
对于iOS 9,上述程序无效。在info.plist中添加以下代码段:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
答案 1 :(得分:8)
我假设您在serviceURL中使用https方案,并且您的测试服务器存在SSL证书问题。如果是这样且您信任它,请在NSURLConnection
委托中实施下一个方法:
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return YES;
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if([challenge.protectionSpace.host isEqualToString:@"127.0.0.1"] /*check if this is host you trust: */ )
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}
让代理人使用NSURLConnection
方法初始化您的initWithRequest:delegate:startImmediately:
。
答案 2 :(得分:1)
在iOS 9.0中,将以下内容添加到info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key><true/>
</dict>
答案 3 :(得分:0)
检查您的代表是否实际被调用。
本文档说明在某些情况下您的代表可能不会被调用。
重要说明:除非服务器响应包含WWW-Authenticate标头,否则URL加载系统类不会调用其委托来处理请求质询。其他身份验证类型(例如代理身份验证和TLS信任验证)不需要此标头。