ASIHTTPRequest "A connection failure occurred" error
2013-03-01 09:31
651 查看
iOS
Development: Why do I always get the “A connection failure occurred” on the 1st attempt, but success on the next?
I'm using the ASIHTTPRequest lib in my iOS app to make RESTful requests to my Rails 3 web app. I seeing a weird and somewhat consistent error the 1st time I try to make a POST request to my web app, but then the POST request works fine the on the second attempt.The exact error is...
Error Domain=ASIHTTPRequestErrorDomain Code=1 "A connection failure occurred" UserInfo=0xb513740 {NSUnderlyingError=0xb5135a0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1005.)", NSLocalizedDescription=A connection failure occurred}
And here's my ASIHTTPRequest code for making the POST request...
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://myrails3app.heroku.com/tournaments/%d/register.json", tid]]; __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; [request setRequestMethod:@"POST"]; [request addPostValue:username forKey:@"username"]; [request setCompletionBlock:^ { NSData *responseData = [request responseData]; NSLog(@"Success!"); }]; // Set the code to be called when the request fails [request setFailedBlock:^ { NSError *error = [request error]; NSLog(@"Error: %@", [error localizedDescription]); }]; // Start the request [request startAsynchronous];
It's worth mentioning that when it errors out, it errors out incredibly quickly! Also, for what it's worth, my Rail 3 app that I'm making the POST request to is hosted on Heroku. Your thoughts?
Answer:
This issue I had a lot of hard time to figure out why. The problem resides in ASIHTTPRequest itself (iOS), not the rails code.
To make a long story short, the problem is specific to the use of persistent connection for every request sent by
ASIHTTPRequest.
While this is good for GET requests, most server implementation does not allow persistent connection to be used with POST request.
I didn't really have time to investigate it deeply on the server side of things but I think that the problem resides with the 100-Continue header that should be sent (and which isn't) with request that has body attached to it (hence PUT/POST). If you want to
have a deeper look at what I'm talking about go have a read at the spec sheet: http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html
(代码默认是最大4个并发连接,其他的连接需要等待,然后这里面有连接请求完毕,就去复用这个连接,但现在在复用连接的时候提示连接关闭,然后重新请求一次连接,如果刚好又有一个连接返回,会自动去复用这个连接,但还是会出现连接关闭的情况,这个时候代码不会再一次重新请求,导致这次请求失败,返回nil。)
So the persistent connection used by ASIHTTPRequest wait for a 100 response to be sent, which is never sent. so it ends up being timed out.
A fix is to set persistentConnection to NO with your post requests like the following:
ASIHTTPRequest *req = [ASIHTTPRequest requestWithURL:url]; req.shouldAttemptPersistentConnection = NO;
Ref:
http://stackoverflow.com/questions/6082471/ios-development-why-do-i-always-get-the-a-connection-failure-occurred-on-the
相关文章推荐
- ASIHTTPRequest "A connection failure occurred" error
- ASIHTTPRequest "A connection failure occurred" error
- How to deal with "Incorrect NSStringEncoding value 0×0000 Error with ASIHTTPRequest" problem?
- Error Domain=ASIHTTPRequestErrorDomain Code=5 "Unable to create request (bad url?)" UserInfo=0x15f86
- "A transport-level error has occurred when sending the request to the server"的解决办法
- 使用ASIHTTPRequest 编译提示找不到"libxml/HTMLparser.h"的解决方法
- Getting error "No subject alternative names" when doing secure URL connection(针对处理https连接的安全异常)(转:http://www.coderanch.com/t/134
- Error "An error occurred while Internet Connection Sharing was being enabled. (null)"
- 使用ASIHTTPRequest 编译提示找不到"libxml/HTMLparser.h"的解决方法
- 问题解决:Error -27492: "HttpSendRequest" failed, Windows error code=12029
- 使用ASIHTTPRequest 编译提示找不到"libxml/HTMLparser.h"的解决方法
- 使用ASIHTTPRequest 编译提示找不到"libxml/HTMLparser.h"的解决方法
- Error Domain=ASIHTTPRequestErrorDomain Code=8 "Failed to move file from"xxx/xxx"to"xxx/xxx"
- ASIHTTPRequest导入出错-libxml出错, i386 "_deflate"
- 使用ASIHTTPRequest 编译提示找不到"libxml/HTMLparser.h"的解决方法
- drupal中"HTTP request status Fails" error解决
- 解决方法:faultDetail = "Channel.Connect.Failed error NetConnection.Call.Failed: HTTP: Failed: url:
- An internal error occurred during: "Map/Reduce location status updater". java.lang.NullPointerExcept
- Error:Unable to tunnel through proxy. Proxy returns "HTTP/1.1 400 Bad Request"
- An internal error occurred during: "Add Deployment"