开发者

ASIHTTPRequest how to recognize finished request and failed request?

In my application, i have a button wich calls an ASIHTTPRequest. The request goes fine and i receive an response string. But when the request finishes, it always goes to the method: requestFinished. And i also got a requestFailed method. But even when i give a wrong link, the request finsihes and never fails.. Why is this? This is my code:

-(void)fetchForm:(id)sender {

    NSURL *URL=[NSURL URLWithString:@"http://www.mydomain.nl/testGet.php"]; 
    ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:URL] autorelease];
    [request setDelegate:self];
    [request setDidFailSelector:@selector(requestFailed:)];
    [request startAsynchronous];

}

-(void)requestFinished:(ASIHTTPRequest *)request {
    NSLog(@"Request Success!");
}

-(void)requestFailed:(ASIHTTPRequest *)request {
    NSLog(@"Request Failed!");
}

EDIT:

开发者_如何学JAVA

I read a little documentation on the ASIHTTPRequest website. And i came to the conclusion that i need to see for myself if there is an error code. i do this with:

int statusCode = [request responseStatusCode];

if (statusCode == 404) {
NSLog(@"Statuscode 404 has occured!");
}


There are several conditions that might affect error reporting with HTTP requests. From ASIHTTP's viewpoint, if the request object can be successfully built, sent, and a some kind of response is received, then everything is ok.

In my case, for example, my ISP has a proxy that will return an error page with many not existing URLs and sometimes even with ill-formed URLs. In such cases, ASIHTTP will not fail. I don't know if this is also your case, but it was for me.

If you look at the file ASIHTTPRequest.m and search for failWithError, you will see all the cases where ASIHTTP will trigger the mechanism that leads to the didFailSelector to be called. You might even set a breakpoint in the failWithError method to see if it is called.

EDIT:

In a sense ASIHTTPRequest mechanism is very basic and covers failures at the network level. If you receive a response then it is an application level failure and you have to deal with it.

First thing is checking the HTTP status code:

int statusCode = [request responseStatusCode];
NSString *statusMessage = [request responseStatusMessage];

This will allow you to identify 404, 500, and so on.

If this does not work and the server does not send an error code, then the only way to go about it is parsing the response you receive and, if it does not contain the data you were waiting for, fail.


try this one -

[request setDidFailSelector:@selector(requestFailed:)];


try this:-

    [request setDidFinishSelector:@selector(requestFinished:)];
[request setDidFailSelector:@selector(requestFailed:)];

Write these two lines and then try.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜