我已经坚持了一段时间,似乎并没有解决这个问题.
我试图从URL读取URL的内容作为字符串,但我得到一个奇怪的
错误 - >错误域= NSCocoaErrorDomain代码= 256"操作无法完成.(Cocoa错误256.)"
我的代码:
fetchedString = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"www.example.com/iphone"] encoding:NSUTF8StringEncoding error:&error];
NSLog(@"%@",fetchedString);
// if there is something wrong with the URL
if (error) {
NSLog(@"Error -> %@", error);
return ;
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我尝试使用获取作为NSData,但我得到null.
我使用Cordova 3.2和jquery mobile 1.3创建了一个iOS应用程序.我使用jquery ajax将请求发送到RESTfull服务以检索/更新信息.
我有许多测试设备,各种iPhone,各种不同的iOS版本.在运行iOS 7的iPhone 4S上,当发送任何ajax请求时,我收到以下错误:
NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
Run Code Online (Sandbox Code Playgroud)
这只发生在这台设备上,在另一台运行iOS 7的iPhone 4S上,它运行得非常好,就像我正在使用的所有其他测试设备一样.
我已经读过,这表明证书在某种程度上是无效的,在这种情况下,这可以被忽略,为什么会影响一台设备?
任何人都可以建议为什么会发生这种情况,以及阻止它仅在我的某个设备上工作的设备有什么不同.谢谢
我正在尝试访问https协议上提供的Web服务.最初我收到以下错误:
NSURLSession/NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9802)errorAn发生SSL错误,无法建立与服务器的安全连接.
我通过在info.plist中添加以下内容来修复它:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>xx.xx.xxx.xxx</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
Run Code Online (Sandbox Code Playgroud)
但是现在我在connectionDidFinishLoading委托方法中得到以下html作为响应:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我正在使用以下来与服务器建立信任:
func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool{
return true
}
func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge){
print("willSendRequestForAuthenticationChallenge")
let protectionSpace:NSURLProtectionSpace = challenge.protectionSpace
let sender: NSURLAuthenticationChallengeSender? = challenge.sender
if(protectionSpace.authenticationMethod == …Run Code Online (Sandbox Code Playgroud) 我在swift中使用Socket.IO库并且我一直收到此错误:
App Transport Security已阻止明文HTTP(http://)资源加载,因为它不安全.可以通过应用程序的Info.plist文件配置临时例外.
当我尝试发送http请求时.我根据官方的苹果文档添加了plist键,但它没有帮助.
我是Objective-c的新手,我在使用AFNetworking时遇到了困难.
所以问题是我想向服务器发一个简单的POST请求,该服务器会给我发回盐.我做一个简单的应用程序,以测试我的请求,但我不明白为什么我得到错误代码999.
这是我的代码示例.
+ (void)simpleRequest; {
NSURL *mailserver = [NSURL URLWithString:@"https://localhost:4443/"];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithBaseURL:mailserver];
manager.securityPolicy.allowInvalidCertificates = TRUE;
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSDictionary *parameters = @{@"username": @"testtest"};
[manager POST:@"api/v0/login/salt" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(NSURLSessionDataTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
Run Code Online (Sandbox Code Playgroud)
我已将此代码链接到一个调用此函数的简单Button.
我有另一个应用程序,在红宝石运动中,这项功能的工作被罚款我可以得到没有任何错误的响应.但是使用这个简单的应用程序,我无法做任何请求,他们都返回此错误代码999.
错误:错误域= NSURLErrorDomain代码= -999"已取消"UserInfo = {NSErrorFailingURLKey = https:// localhost:4443/api/v0/login/salt,NSLocalizedDescription =已取消,NSErrorFailingURLStringKey = https:// localhost:4443/api/v0/login/salt }
所以我真的很想知道我做错了什么,有谁可以帮我这个?谢谢
编辑:
这是在经理中保存经理还是做错事的好方法?如果这是好方法,这似乎不起作用谢谢你的帮助
.h文件
@property (nonatomic, retain) AFHTTPSessionManager *session;
Run Code Online (Sandbox Code Playgroud)
.m文件 …
我正在为JSON运行$ http.get并且状态为0.我已经下载了相同的JSON并且get在本地工作,而在Python中使用请求库我可以得到JSON没问题,但是AngularJS它没有用.我不明白为什么角度没有得到它,但其他一切都是.下面的代码片段.
function AgentListCtrl($scope, $http) {
$http.get('http://foo.bar/api/objects').success(function(data) {
$scope.objects = data;
}).error(function(data, status) {
$scope.status1 = status;
});
Run Code Online (Sandbox Code Playgroud)
这提供了JSON并在使用本地文件时对其进行解析,但否则会失败并将status1设置为0.
我正在尝试使用iOS 9+ 加载https://网页UIWebView,它会出现以下错误:
NSURLSession/NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9802)CFNetwork SSLHandshake失败(-9802)
我已经尝试过各种NSURLConnection使用connection:willSendRequestForAuthenticationChallenge:方法首先使用处理身份验证质询的方法.
虽然这对于直接请求工作正常,但是 (这里实际问题开始)这不适用于网页重定向到具有不同域的另一个页面的情况.
因此,例如,最初我加载UIWebViewwith调用,https://example.com然后该页面重定向到https://someothersecuredomain.com.这是连接失败而connection:willSendRequestForAuthenticationChallenge:未被调用.
以下是我的实施:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Load webview with request url and parameters
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPBody:postBody];
[request setHTTPMethod: @"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
self.webView.delegate = self;
[self.webView loadRequest: request];
_BaseRequestURL = url;
}
#pragma mark UIWebView delegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (![[request.URL absoluteString] …Run Code Online (Sandbox Code Playgroud) 重要信息,我在TestCase下运行所有这些.
我的网址是 http://my-MacBook-Pro.local:8080/myapi/v2/Driver/getDriver?domain=123&driver=125&key=9808098
如果我在Safari运行时粘贴此URL iPhone Simulator.它以JSON响应.我注意到我的catalina.out报告了这个请求.
我启用Allow HTTP Services了Settings->Developer.
这是代码段
print("fullurl ->"+urlComponents.url!.absoluteString)
URLSession.shared.dataTask(with: urlComponents.url!, completionHandler: {
(data, response, error) in
if(error != nil){
print("error")
}else{
do{
let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String : AnyObject]
print (json)
}catch let error as NSError{
print(error)
}
}
}).resume()
Run Code Online (Sandbox Code Playgroud)
编辑
参考
添加以下内容info.plist,仍然我的http请求没有通过localhost运行tomcat的我的macport 8080
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>My-MacBook-Pro.local</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
Run Code Online (Sandbox Code Playgroud)
编辑
即使使用这些设置也可以尝试 …
我无法在Xcode 9.2中禁用App Transport Security(ATS).我(多年来)在针对我的本地服务器环境运行构建时禁用ATS,如下所示:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Run Code Online (Sandbox Code Playgroud)
在Xcode 9.2中,一个简单的请求(在http模式下针对本地Rails应用程序运行):
let session = URLSession(configuration: .default)
let url = URL(string: "http://store.dev/api/products.json")!
let task = session.dataTask(with: url) { data, response, error in
print(data)
print(response)
print(error)
}
task.resume()
Run Code Online (Sandbox Code Playgroud)
失败并显示错误消息
错误域= NSURLErrorDomain代码= -1200"发生SSL错误,无法与服务器建立安全连接." UserInfo = {_ kCFStreamErrorCodeKey = -9802,NSLocalizedRecoverySuggestion =您是否还要连接到服务器?,NSUnderlyingError = 0x60c00024afb0 {错误域= kCFErrorDomainCFNetwork代码= -1200"(null)"UserInfo = {_ kCFStreamPropertySSLClientCertificateState = 0,_kCFNetworkCFStreamSSLErrorOriginalValue = -9802, _kCFStreamErrorDomainKey = 3,_kCFStreamErrorCodeKey = -9802}},NSLocalizedDescription =发生了SSL错误,无法建立与服务器的安全连接.,NSErrorFailingURLKey = https://store.dev/api/products.json,NSErrorFailingURLStringKey = https: //store.dev/api/products.json,_kCFStreamErrorDomainKey = 3}
这个完全相同的请求(同样的项目)在Xcode 9.1上取得了成功.
在这两种情况下,我都是针对iOS 11.1部署目标而构建的.您可以看到Xcode正在将URL从http更改为https,这是我不想要的.
以下是在Xcode 9.1中工作的超级基础项目的链接,但在9.2( …
不幸的是,今天早上我的XCode更新到版本7,而我用http开发的iOS应用程序现在想要https.因此,在许多教程之后,我配置了我的MAMP服务器,以便使用https/ssl创建虚拟证书.现在在我的iOS应用中,URL如下所示:
static var webServerLoginURL = "https://localhost:443/excogitoweb/mobile/loginM.php"
static var webServerGetUserTasks = "https://localhost:443/excogitoweb/mobile/handleTasks.php"
static var webServerGetUsers = "https://localhost:443/excogitoweb/mobile/handleUsers.php"
static var webServerGetProjects = "https://localhost:443/excogitoweb/mobile/handleProjects.php"
Run Code Online (Sandbox Code Playgroud)
如果我尝试在浏览器中访问它们,它们工作正常.我习惯使用NSURLSession.sharedSession().dataTaskWithRequest()来访问数据库和php文件,现在它会引发标题中的错误.例如,这是引发错误的行:
if let responseJSON: [[String: String]] = (try? NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())) as? [[String: String]] {
...
}
Run Code Online (Sandbox Code Playgroud)
这是完整的错误消息:
2015-09-21 16:41:48.354 ExcogitoWeb[75200:476213] CFNetwork SSLHandshake failed (-9824)
2015-09-21 16:41:48.355 ExcogitoWeb[75200:476213] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9824)
fatal error: unexpectedly found nil while unwrapping an Optional value
Run Code Online (Sandbox Code Playgroud)
我想知道如何解决这个问题.我在这里已经阅读了一些有用的答案,但有许多事情我仍然不明白,如果有人愿意帮助/解释我,我将非常感激.