AFNetworking -1017 error - ios

I am working on iOS application and facing one strange issue. I am using AFNetworking framework to communicate with server (HTTPS communication). I am retrieving student data from server by using "getStudentData" web service API. It is post request. It works for all user ids except one. It fails when we have a data for more than 450 students. Below are the error details,
Error Domain=NSURLErrorDomain Code=-1017 "cannot parse response" UserInfo=0x7bf9c7d0 {NSErrorFailingURLStringKey=https://www.fdmobileservices.com/mAccountsWeb/services/speedpass/rpc, _kCFStreamErrorCodeKey=-1, NSErrorFailingURLKey=https://www.fdmobileservices.com/mAccountsWeb/services/speedpass/rpc, NSLocalizedDescription=cannot parse response, _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x7bf9a380 "cannot parse response"}
As per error description, It tells "Could Not parse", so I think it may be due to server is returning "nil" or some data other than JSON format.
But I am not able to trace it since from application side it directly goes into below method,
- (void)connection:(NSURLConnection __unused *)connection
didFailWithError:(NSError *)error
Is there any way from application side to trace root cause? This method works for other user login except one.
I tried using web client to access this service by same user login, it works well and return data of 450 students. I think due to some reason iOS network layer rejects this. I am trying to find out.
Thanks in advance.

As you can see here, kCFURLErrorCannotParseResponse = -1017. So probably this is saying that there is something wrong in the parameters.
If You're using AFNetworking 2.0, I suggest You to deep debug your response flow with severals breakpoints.
Specifically try to set a breakpoint on AFURLResponseSerialization.m at the start of the method:
- (id)responseObjectForResponse:(NSURLResponse *)response
data:(NSData *)data
error:(NSError *__autoreleasing *)error;
Inside the method, if the below condition is true, probably there is something wrong (but you can step into validateResponse:data:error: to better understand what's wrong):
if (![self validateResponse:(NSHTTPURLResponse *)response data:data error:error])
If the above condition is false you can then check about the generated responseString:
NSString *responseString = [[NSString alloc] initWithData:data encoding:stringEncoding];
If you're expecting json data, check that responseString is not nil and data data is valid, maybe also the stringEncoding.
Also ensure that the contentType that you're receiving is the one you're expecting.

Related

Authentication Failed Error - XMPPFramework - not-authorized

After successfully connecting to the server, ie. after a successful callback to
- (void) xmppStreamDidConnect:(XMPPStream *)sender
I sometimes get an authentication failure, ie. the following callback is called:
(void) xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error
The NSXMLElement looks like this:
<failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><not-authorized/><text>The response provided by the client doesn't match the one we calculated.</text></failure>
Like I said, I get this once in a while. I've traced the issue and I've tried several things so far.
Things that's I have looked into and I can confirm so far:
The username and password are 100% correct
I'm using the SCRAM-SHA-1 authentication mechanism (XMPPSCRAMSHA1Authentication)
My server supports the PLAIN mechanism as well and it fails with that as well (XMPPPlainAuthentication)
If I try to force a disconnect when I get this error ([xmppStream disconnect]) and then in the xmppStreamDidDisconnect callback I try to re-open the stream ([xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error]) then I get into an infinite loop because the authentication fails consistently
Now, I looked into this even further and when using the SCRAM-SHA-1 mechanism it looks like it fails on the second challenge. The client sends something (seemingly valid) to the server but the server doesn't like it and throws the not-authorized error.
Thoughts anyone?
You have to check your username and Password. If you are passing wrong username and password for authenticate/Login then this didNotAuthenticate method will be called

Instagram Getting user details

Am trying to integrate instagram in my app,while getting user details after getting access token,its showing error . Am appending data in NSData and trying to print it in connectionDidFinishLoading(NSURLConnection *)connection ( NSURLConnection Delegate) . While converting NSData to id ,its showing null .Also if i am converting NSdata to NSString am getting Response as
{"meta":{"error_type":"OAuthAccessTokenException","code":400,"error_message":"The access_token provided is invalid."}}{"meta":{"code":200},"data":{"username":"XXXX","bio":"","website":"","profile_picture":"http:sampleiamge.jpg","full_name":"samplename","counts":{"media":1,"followed_by":0,"follows":0},"id":"sampleID"}}
If am printing the json as id jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
getting error as
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be
completed. (Cocoa error 3840.)" (Garbage at end.) UserInfo=0xa967bc0
{NSDebugDescription=Garbage at end.}
Tried all jsonreading option ,but no result
Any hep will be appreciable.
Thanks
You could also have a look at the InstagramKit which does this fairly straight forward. https://github.com/shyambhat/InstagramKit
There's a demo app so you can see how the authentication is done and request a user's details in just a few lines of code. You don't really need to mess around with the access token data format.
It seems like you are not authenticate.
There is a project in GitHub that implements a simple UIViewController that performs the Instagram auth process step by step:
https://github.com/Buza/Instagram-Auth-iOS
You can see all process from request token to error management.
Hope it helps!
The response occurs when the access_token is invalid, try printing (NSLog) out the URL your are making the request and make sure the access_token is correctly passed.

iOS url response bool

net web service that returns true or false but i don't know how to catch that response in my IOS App.
My service updates data in a database and i know it works the data gets updated it's catch the response that is the problem, i like to know so i can tell the user if something went wrong.
For those of you that know c# its a bool method, just simple try catch and return true or false.
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
//What to write here to catch my true or false
if(response) {
//true
} else {
//false
}
}
Thank you for your help
You should implementconnection:didReceiveData: to get and save NSData and – connectionDidFinishLoading: where you can interpret received data as BOOL.
basically didReceiveResponse: only get to you know about server response to your request not the entire answer.
You should check the response's HTTP status code, e.g.:
NSInteger statusCode = [(NSHTTPURLResponse*)response statusCode];
The status code for a successful request uses the range [200..299].
For example a successful GET request would be indicated with a 200 (OK).
A successful POST request will be indicated with a 201 (Created).
A successful DELET request will be indicated with a 204 (No Content)..
See also: wiki List of HTTP status codes.
Furthermore, you need to check the kind of data the server sent to you:
NSString* mimeType = [response MIMEType];
The mime type has been sent by the server in the Content-Type header of the response.
See also wiki MIME Internet Media Type
What you actually get fully depends on your request AND the server.
For example, the server may always answer with a JSON as content type. In this case, the header Content-Type of the response would be application/json. The actual JSON which represents the answer, will be related to the status code as well.
In order to provide a nice human readable message to the user, you need to consult the web service API and figure out how it is specified. Certain web service APIs may have a considerable large API. Unfortunately, some web services lack a comprehensive documentation.

Read whole html string Asyncsocket iOS

Hi I want to read from a server the whole html not only the request like
"HTTP/1.1 200 OK"
I mean the html code too.
But I dont know how.
I am using the Asyncsocket library from "https://github.com/roustem/AsyncSocket"
have someone an idea how can i handle this?
i used the funktion
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
but the data is to short and show me only the http request.
Thanks
I would guess that you are calling readDataToData:timeout:tag: using CRLF as the delimiter. That will return you one line of data. What you want is to read enough headers to tell how long the response data ought to be and then call readDataToLength:timeout:tag:, or else wait for the connection to be closed and call unreadData to get everything the server sent up to when it closed the connection.
The HTTP spec describes how to determine the length of the message: https://www.rfc-editor.org/rfc/rfc2616#section-4.4
A better answer might be to use NSURLConnection to make the request and interpret the response for you.

With iOS asynchronous http request, how do you connect the received data to the request?

I'm loading certain images from a certain server asynchronously. I'm firing a number of requests at once using NSURLConnection connectionWithRequest method and receive the data using NSURLConnectionDelegate didReceiveData.
At didReceiveData, how do I know which request this data matches? At didReceiveResponse I can use the URL method of the response given as a parameter, but in didReceiveData I only have the received data.
It seemed like the perfect answer would be to use NSURLConnection sendAsynchronousRequest, as the completion handler has all the required parameters: (NSURLResponse*, NSData*, NSError*). I can use [response URL] to make a match to the original request... except in one case: not all the images I try to download exist. In that case, the request is redirected to a generic error page and the URL of that generic page is received, so I can't match the response to a request I've made. This I could handle with connectionWithRequest, but I can't get the best of both worlds.
In
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
you can use
NSURLRequest *request = [connection originalRequest];
to get the request that the connection was started with.
(This method is available since iOS 5.0, but I could not find it in my Xcode iOS 5.1 Library. You find it in the iOS 6 Library, in the header file NSURLConnection.h or here: http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html#//apple_ref/occ/instm/NSURLConnection/originalRequest).
More methods to manage multiple connections can be found in this thread: Managing multiple asynchronous NSURLConnection connections.
If you use sendAsynchronousRequest:queue:completionHandler: then you can just use the request parameter in the completion block.
At connection:didReceiveData: the first parameter is the NSURLConnection instance. So I don't understand where the problem is. You create a connection, then you send a request to that connection and the delegate receive the connection:didReceiveData with the connection value.
If you are using the same delegate for all the request you have to check the connection so you can say which request is associated to.
Perhaps you have to maintain a table of connection/request pairs.

Resources