dubbeat
dubbeat

Reputation: 7847

HTTP POST request not working, delegate methods are not being called

I’m wondering why this HTTP POST request isn’t working in my iPhone app.

I know for a fact that the URL is correct and that the variables I’m sending are correct, but for some reason the request is not being received by the .aspx page.

EDIT:

I refactored the code into its own class, with its own delegate methods. But the delegate methods are not being called.

The class is called like this:

URLCallClass *reporter=[[[URLCallClass alloc] init]autorelease];
    [reporter sendoview:@"http://mysite/page.aspx" params:httpBodyString];

and this is the actual class itself:

-(void)sendview:(NSString *)server params:(NSString *)params
{

    NSURL* url=[[NSURL alloc] initWithString:server];   
    NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:url];
    [url release];

    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];

    connectionResponse=[[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self] autorelease];
    //NSURLConnection *connectionResponse = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];


    if (!connectionResponse)
    {
        NSLog(@"Failed to submit request");
    }
    else
    {
        NSLog(@"---------Report  Request submitted ---------");
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"report received response");

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
   }

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"report failed with error");
    NSLog(@"%@", [error description]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"Promo View Reported");
}

-(void)dealloc

{
    [connectionResponse release];

    [super dealloc];

}

Upvotes: 0

Views: 731

Answers (4)

dubbeat
dubbeat

Reputation: 7847

It turns it had to do with threading

NSURLConnection delegation and threading - iPhone

I used this solution

http://www.depl0y.com/?p=345

Upvotes: 0

emmanuel.aquino
emmanuel.aquino

Reputation: 851

Are you printing the response headers? I would suggest that you implement almost every delegate method to check what is actually happening with your connection, try first printing the didReceiveResponse Method, and some of the data you're receiving using the connection:didReceiveData method.

And perhaps the encoding of your data is something the server cannot process, try using NSUTF8StringEncoding instead of NSISOLatin1StringEncoding as the body encoding.

Upvotes: 0

jessecurry
jessecurry

Reputation: 22116

Is your url actually being created? If there is any problem NSURLs initWithString: method will return nil.

Upvotes: 0

cem
cem

Reputation: 3321

You should implement the connection:didFailWithError: in the delegate.

NSLog(@"%@", [error description]); will show you, what exactly went wrong.

Upvotes: 2

Related Questions