Error Domain=NSURLErrorDomain Code=-1012 "(null)" in iOS Objective C - ios

I requested to server with POST method, and server response me 401 status code. i have error 1012 and my response is nil.
//Send request
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
Therefore i found some code for disable and change security mode in iOS Objective C request but is don't know how to use it and where set code.
please help
AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
[policy setValidatesDomainName:NO];
[policy setAllowInvalidCertificates:YES];
[policy setValidatesCertificateChain:NO];

According to the docs:
NSURLErrorUserCancelledAuthentication = -1012
Here is the error list
More over HTTP status code 401 = Unauthorized

First of all, do not disable certificate validation. It will not solve your problem. The server is rejecting the client's authentication, not the other way around.
Second, your problem is that the user's credentials are not in the keychain and you aren't providing delegate methods to provide credentials yourself. I'm not sure precisely how you do that with the AF API, but you can read about the way you would do it directly at https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/AuthenticationChallenges.html and use that as a starting point for understanding what is happening under the hood.

Related

AFNetworking 2.0 - unexpected NSURLErrorDomain error -1012

We ran into the following issue with our app that uses AFNetworking 2.0.
When using AFHTTPRequestOperationManager's GET method, we got an error NSURLErrorDomain code -1012. The request used HTTPS and the server does not require user authentication. The request never reached the server by the way.
We have run several tests and this is the first time the error was produced and we are wondering how this error can get produced because it does not seem relevant.
Setup of AFHTTPRequestOperationManager :
httpOperationManager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:
[NSURL URLWithString: HTTPS_URL)]];
httpOperationManager.responseSerializer =
[AFXMLParserResponseSerializer serializer];
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled: YES];
GET REQUEST
AFHTTPRequestOperation *op =[httpOperationManager GET:
[NSString stringWithFormat:SOME_PATH]
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
//code to setup NSXMLParser ...
}
failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"error %#", [error localizedDescription]);
}];
I think you already solved the problem, but if you are trying to authenticate in a server that doesn't have a valid certificate you have to set YES for property allowInvalidCertificates in your AFHTTPRequestOperationManager object:
[yourManager.requestSerializer setAuthorizationHeaderFieldWithUsername:#"your_username" password:#"your_password"];
[yourManager.securityPolicy setAllowInvalidCertificates:YES];
Also, as #a1phanumeric said, it can be necessary to include this line:
[yourManager.securityPolicy setValidatesDomainName:NO];
Cheers.
NSURLErrorDomain -1012 is NSURLErrorUserCancelledAuthentication. (See the error code list and search for -1012.)
You state, "the server does not require user authentication". But this error would not be called if that were true.
Possible causes:
Your server is erroneously requesting authorization (a server bug)
The URL formed with HTTPS_URL and SOME_PATH is not what you expect, and some other server is requesting authorization
Some intermediary (like a proxy server, or an access point) is requiring authorization.
Some debugging tips:
Set breakpoints inside the AFNetworking implementation to see which URL is being hit
Configure AFHTTPRequestOperationLogger so you can see the actual request body and response in your console log
Make the same request with curl or Advanced Rest Client and observe the server's response
Side note: I think [NSString stringWithFormat:SOME_PATH] is pointless - why not just use SOME_PATH?

ios - Restkit and SSL certificate error

I have a tomcat server that uses a self signed SSL certificate and is running a web service. I am trying to connect to the web service with Restkit. However, I am getting an error related to certificate validity. Here is my code:
NSURL *url=[NSURL URLWithString:baseURL];
RKClient *client = [RKClient clientWithBaseURL:url];
client.disableCertificateValidation=YES;
RKRequest *request = [client requestWithResourcePath:[NSString stringWithFormat:#"/addEvent?deviceID=%#&eventID=%#",deviceID,eventID]];
request.disableCertificateValidation=YES;
request.delegate=self;
RKResponse *response = [request sendSynchronously];
This request fails with the following error:
2013-01-09 15:11:53.931 Mobile_ACPL[5761:907] The certificate for this server is invalid. You might be connecting to a server that is pretending to be “notify.acpl.lib.in.us” which could put your confidential information at risk.
I get this error even though I have set disableCertificateValidation to YES. How can I get this working?
EDIT: I attempted adding the certificate as shown here: https://github.com/RestKit/RestKit/pull/131
I still get the same result.
EDIT 2: It looks like the error message is being set at this line in RKRequest.m:
payload = [NSURLConnection sendSynchronousRequest:_URLRequest returningResponse:&URLResponse error:&error];
NSURLConnection does not cater for authentication challenges in synchronous calls. You need to make asynchronous calls for this to work.
In my case, I was setting disableCertificateValidation on RKClient but I was using a RKObjectManager which used a different RKClient. The following line, placed after the RKObjectManager initialization, did the trick:
[RKObjectManager sharedManager].client.disableCertificateValidation = YES;
if you are using RestKit using
client.allowsInvalidSSLCertificate = YES;
won't work, instead do this:
if you added rest kit manually to your project, click on RestKit.xcodeproj go to project > Build Settings > Preprocessor Macros
and add _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_=1
thats finished.

ASIHTTPRequest GET Request Issue

My request is
request_ = [[ASIFormDataRequest requestWithURL:requestUrl] retain];
[request_ setDelegate:self];
[request_ setRequestMethod:#"GET"];
[request_ setTimeOutSeconds:HTTP_TIME_OUT];
[request_ startAsynchronous];
But the response from the server is
HTTP Status 405 - Request method 'GET' not supported. The specified HTTP method is not allowed for the requested resource (Request method 'GET' not supported).
Please note that the url doesn't have any "GET" parameters along with it even though it is a get request. The thing is I am getting the proper response from the server when simply I take the request URL in a browser or when I call it using "HttpRequester"(a Firefox add-on to test http requests - well I'm sure you know that). What could have went wrong?
Thanks in advance.
Balu
For GET requests, use ASIHTTPRequest.
ASIFormDataRequest is for POST requests.
However, if all you need is a simple GET request, why bother with ASI? You can do this in a dispatch_async block:
dispatch_async(<some_queue>, ^{
NSError * error = nil;
NSString * response = [NSString stringWithContentsOfURL: stringWithContentsOfURL: requestUrl error: &error];
[self performSelectorOnMainThread: #selector(processResult:) withObject: response waitUntilDone: NO];
});
Solved it. It was because of the absence of the header parameter "Accept" in requests that have been made. Once it was added, everything worked like a charm.
Also replaced ASIFormDataRequest with ASIHTTPRequest(that one was a silly mistake).
Seems it is a server side issue. Please verify if all the header parameters as required by the server are there in request.
why you create a ASIFormDataRequest aka POSTRequest and set the RequestMethod to GET?
If you want to make a GET Request use ASIHTTPRequest instead.

SSErrorDomain error code 109. NewsStand download from Amazon S3

While downloading a file from Amazon S3 in iOS. Intermittently NSURLConnectionDownloadDelegate's method didFailWithError: get called and this is what I got when I logged received NSError object
Error Code: 109
Error Domain: SSErrorDomain
Error Description: "Cannot connect to .s3.amazonaws.com"
Searched all the Apple documentation, StackOverflow and other sites but not found anything on this. Today I raised a technical query to Apple also for this using my developer account.
Any idea ?
Update:
So after looking into HTTP response error code (403 Forbidden), I got the idea. It is because of "RequestTimeTooSkewed" error from S3 (The difference between the request time and the current time is too large.). I cross checked it by changing iPad's/Mac's system time by 1 hour and this error is coming immediately now, even for a small (200kb) file.
Now as suggested in many blogs I am first making a HEAD request to AWS as below to get the Date string and not passing the system Date
NSString *awsURL = #"http://s3.amazonaws.com";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:awsURL]];
[request setHTTPMethod:#"HEAD"];
NSHTTPURLResponse *response;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error: NULL];
NSString *dateString = nil;
if ([response respondsToSelector:#selector(allHeaderFields)]) {
dateString = [[response allHeaderFields] objectForKey:#"Date"];
}
return dateString;
and setting this as Date header in NSMutableURLRequest
[urlRequest setValue:awsDateString forHTTPHeaderField:#"Date"];
This request I am adding to my issue for download
NKAssetDownload *nkAssetDownload = [nkIssue addAssetWithRequest:urlRequest];
Still the same error !!!! It now more crazier than my last situation.
Anyone ?
Update 2
I was able to make request successfully (even the system clock of my iPad is incorrect) by replacing "GMT" with "+0000" in the date string.
Update 3
Still some requests fail with same error which is weird, but I am assuming it is something the NewsStand Framework is messing up.
So it is RequestTimeTooSkewed error and the above code to fetch date from S3 server's head response to add in request does the trick.

RestKit Login problem

Can anyone give me a hint why I can not login into my service? I dont get an response to my delegate. The service is working. If I call the url in my browser I get a login (Browser receives a cookie) and then I can call request on my service:
Here is my Code:
RKClient *client =[RKClient clientWithBaseURL:#"https://myserverinstance/mobileapp/"];
NSString *loginString = #"SSO.login?application=myAppName&language=en&username=MyUserName&password=MyPassword&permanentLogin=true";
[RKClient setSharedClient:client];
if([[RKClient sharedClient] isNetworkAvailable]){;
NSLog(#"Network is available");
[[RKClient sharedClient] get:loginString delegate:self];
I'm assuming that the typos in your posted code (e.g. if([[RKClient sharedClient] isNetworkAvailable]){;) do not exist in your actual project.
Are your SSL certificates self-signed? If so, you'll need to set the RESTKIT_SSL_VALIDATION preprocessor directive. You'll also need to link against Security.framework.
If your certificates are signed by a common authority, then you should post more information about your problem. Turn RestKit logging on with RKLogConfigureByName("RestKit/Network", RKLogLevelTrace); and see if that provides any hints to where the problem really lies.

Resources