views:

200

answers:

4

Hi,

I'm wondering why this http post request won't work for 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 recieved by the aspx page.

EDIT::

I re factored the code into its own class with its own delegate methods. The delegate methods do not get called though

The class is called like so

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];

}
+2  A: 

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

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

unset
the didfailWithError handler never gets called
dubbeat
A: 

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

jessecurry
I commented out all of my code except for the code in question and it worked find. I use another NSURLConnection in the same class. It gets executed before this one. Could that be causing the problem somehow?
dubbeat
url is being successfully created
dubbeat
A: 

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.

emmanuel.aquino
A: 

It turns it had to do with threading

http://stackoverflow.com/questions/1216304/nsurlconnection-delegation-and-threading-iphone

I used this solution

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

dubbeat
So to clarify: all of this code was running on a thread other than the main thread?
JeremyP
Well there was UIView Running on the main thread and the httprequest code was in an instance of class in this UIView. I believe this http request class exited before completing its async urlconnection once the parent UIView function that called it finished executing the method that called it.So I dont think it was on the main thread but the class that called it was.I could be way off here though, this is quite new to me and I'm still learning.
dubbeat